"David Smit" <davidsmit@gmail.com> wrote in
message
news:a12df23466bc1422039931239cdbd144@ruby-forum.com...> Hi,
>
> I am trying to add search functionality to may application. I am
> struggeling to pass my query to the controller and then show all my
> search results in another view. How would I do this?
>
> Model:
> class Advert < ActiveRecord::Base
> belongs_to :user
>
> def self.search(query)
> if !query.to_s.strip.empty?
> tokens = query.split.collect {|c| "%#{c.downcase}%"}
> find_by_sql(["select s.* from songs s where #{
> (["(lower(s.song_title) like ? or lower(s.song_lyrics) like ?)"]
*
> tokens.size).join(" and ") } order by s.created_on desc",
*(tokens *
> 2).sort])
> else
> []
> end
> end
> end
>
> Controller:
>
> def search
> @query = @params["query"]
> @adverts = Advert.search(@query)
> end
>
> #Searches for Adverts
> def search_results
> @query = @params["query"]
> @adverts = Advert.search(@query)
> end
>
> View:
> search.rhtml:
> <%= form_tag %>
> <table>
> <tr>
> <td>Search:</td>
> <td><%= text_field("advert", "query")
%></td>
> <td><input type="submit" value="Search"
/></td>
> </tr>
> </table>
>
> <%= end_form_tag %>
>
> search_results.rhtml:
> Search results for: <%=h @query %>
>
> Thanks,
>
> David
>
> --
> Posted via http://www.ruby-forum.com/.
david, correct me if I''m wrong but it sounds like you want the user to
search from /search and you want to display the results on /search_results ?
if that''s the case, just change search.rhtml so the query
get''s POST''d to
/search_results, so instead of :
<%= form_tag %>
you''d have
<%= form_tag({:action => :search_results}) %>