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
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
Leo Kowalsky 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 Kowalsky 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 Kowalsky 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)
<h1>Editing address</h1> <%= error_messages_for :address %> <% form_for(@address) do |f| %> <p> <b>Line1</b><br /> <%= f.text_field :line1 %><%= f.submit "Like a button here?" %> </p> <p> <b>Line2</b><br /> <%= f.text_field :line2 %><%= f.submit "Or here?" %> </p> <p> <b>Line3</b><br /> <%= f.text_field :line3 %><%= f.submit "Or even here?" %> </p> <p><b>Line4</b><br /><%= f.text_field :line4 %></p> <p><b>City</b><br /><%= f.text_field :city %></p> <p><b>State</b><br /><%= f.text_field :state %></p> <p><b>Zip</b><br /><%= f.text_field :zip %></p> <p><%= f.submit "The Submit at the bottom where everyone expects it?" %></p> <% 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