当前位置: 动力学知识库 > 问答 > 编程问答 >

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

<% 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 =>([email protected] = @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
分享给朋友:
您可能感兴趣的文章:
随机阅读: