How to create searchbox

I am wondering how I can put a searchbox at the botom of my index
page to search through the users (by age, city ect…)

I put


<%= text_field_tag :query %>


at the bottom of my page but how do I add a button to handle the content
of the searchbox so that I can use it as filter?

I want to recuperate the value in the searchbox and put in my users
controler like this :


if(params[:filter])
@users = User.find(:all, :conditions => “users.city =
‘#{params[:filter]}’”)
else
@users = User.find(:all)
end


thanks in advance !

Leo K. wrote:

I am wondering how I can put a searchbox at the botom of my index
page to search through the users (by age, city ect…)

I put


<%= text_field_tag :query %>


at the bottom of my page but how do I add a button to handle the content
of the searchbox so that I can use it as filter?

I want to recuperate the value in the searchbox and put in my users
controler like this :


if(params[:filter])
@users = User.find(:all, :conditions => “users.city =
‘#{params[:filter]}’”)
else
@users = User.find(:all)
end


thanks in advance !

the query field will now be in your parameters. you can address it with
params[:query]. If you are doing a search, you will want something like

if(params[:query])
@users = User.find(:all, :conditions => [‘users.city like ?’,
‘%’+params[:query]+’%’])
else
@users = User.find(:all)
end

hope this helps

I did “like” since it was a search, but if you want an exact match, do
“=” and leave off the “%” on both sides of param[:query]

Leo K. wrote:

@users = User.find(:all, :conditions => “users.city =
‘#{params[:filter]}’”)

The above conditions argument leaves you open to a wealth of SQL
injection attacks.

I strongly recommend leveraging the protection Rails provides by using
the array notation:

:conditions => [ ‘users.city = ?’, params[:filter] ]

Thanks, but how do I create a submit button next to my searchbox to
process the searchinfo?

Leo K. wrote:

Thanks, but how do I create a submit button next to my searchbox to
process the searchinfo?

Hmmm… (just hacking up a form in my tester app)

Editing address

<%= error_messages_for :address %> <% form_for(@address) do |f| %>

Line1
<%= f.text_field :line1 %><%= f.submit "Like a button here?" %>

Line2
<%= f.text_field :line2 %><%= f.submit "Or here?" %>

Line3
<%= f.text_field :line3 %><%= f.submit "Or even here?" %>

Line4
<%= f.text_field :line4 %>

City
<%= f.text_field :city %>

State
<%= f.text_field :state %>

Zip
<%= f.text_field :zip %>

<%= f.submit "The Submit at the bottom where everyone expects it?" %>

<% end %> <%= link_to 'Show', @address %> | <%= link_to 'Back', addresses_path %>

If you don’t have a model associated with it, you can use form_tag. I
have this:

<% form_tag “/store/search” do -%>

<%= text_field_tag :search, params[:search],
:value => session[:search] || “Search Here”,
:onfocus => ‘value=""’, :class => “search” %>

<%= image_submit_tag “buttons/search.gif”,
:class => “noborder” %>

<% end -%>

Make the specified URL correspond to your controller’s search action,
and use the param that you specified in the controller.

-Kyle