on form with multiple search buttons that access different indexes and models

15 views
Skip to first unread message

tyliong

unread,
Jan 22, 2009, 11:19:04 AM1/22/09
to Thinking Sphinx
Hi,

i want to have one form that has multiple search buttons. Something
like what google has (search and I'm feeling lucky). In this case I
want to have one search button to search the model groceries and the
other search button to search customers.

my search view is currently this:

<% form_tag '/search', :method => "get" do %>
<%= text_field_tag("query", params['query'], :size => 30 ) %>
<%= submit_tag "Search" %>
<% end%>
my groceries controller is this:

def search
@grocery = Grocery.search params[:query], {:page => params
[:page], :per_page => SEARCH_PAGE_NO}
end

Pat Allan

unread,
Jan 27, 2009, 12:27:36 AM1/27/09
to thinkin...@googlegroups.com
You need to have both buttons having the same name, but different
values (and for buttons, values are the text). So, maybe something like:

<%= submit_tag "Search Customers", :name => "mode" %>
<%= submit_tag "Search Groceries", :name => "mode" %>


And then in the controller:

def search
klass = case params[:mode]
when "Search Customers"
Customer
when "Search Groceries"
Grocery
end

@results = klass.search params[:query],
:page => params[:page],
:per_page => SEARCH_PAGE_NO
}

# ...
end

Hopefully this helps.

Cheers

--
Pat

Tan YL

unread,
Jan 27, 2009, 1:01:42 AM1/27/09
to thinkin...@googlegroups.com
Thanks for the replies. Got that working.
Reply all
Reply to author
Forward
0 new messages