Rails - Searching multiple textboxes and fields
Posted
by ChrisWesAllen
on Stack Overflow
See other posts from Stack Overflow
or by ChrisWesAllen
Published on 2010-04-06T17:16:56Z
Indexed on
2010/04/06
18:03 UTC
Read the original article
Hit count: 142
I have a model of events that has various information such as date, location, and description of whats going on.
I would like for my users to be able to search through the events list through a set of different textboxes but I having a hard time getting the syntax just right
in my view I have...
<% form_tag users_path, :method => 'get' do %>
(<%= text_field_tag :search_keyword, params[:search_keyword] %>) +
(<%= text_field_tag :search_zip, params[:search_zip] %>)
<%= submit_tag "Find Events!", :name => nil %>
<% end %>
and in the controller I'm trying to query through the results....
if params[:search_keyword]
@events = Event.find(:all, :conditions => [' name LIKE ? ', "%#{params[:search_keyword]}%"])
elsif params[:search_zip]
@events = Event.find(:all, :origin=> params[:search_zip], :within=>50 )
else
@events = Event.find(:all)
end
How do I code it so that it will perform the search only if the textbox isnt empty?
also if both textboxes are filled then @events should be the product of BOTH queries? if have no idea if this would work =>(???@event = @event+ event.find.....???
© Stack Overflow or respective owner