Passing a variable between methods

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 %>

Search: <%= text_field("advert", "query") %>

<%= end_form_tag %>

search_results.rhtml:
Search results for: <%=h @query %>

Thanks,

David

“David S.” [email protected] wrote in
message news:[email protected]

def self.search(query)

end
@query = @params[“query”]

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}) %>

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs