开发者

Rails - Searching multiple textboxes and fields

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

<% f开发者_开发问答orm_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.....???


In this case, the params will always have :search_keyword and :search_zip but the values will be empty/nil/blank. So do something like this:

if !params[:search_keyword].blank? && !params[:search_zip].blank?
  # build query for both
elsif !params[:search_keyword].blank?
  # build for keyword
elsif !params[:search_zip].blank?
  # build for zip
else
  # all
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜