# How to create searchbox

**URL:** <https://www.ruby-forum.com/t/how-to-create-searchbox/137421>\
**Category:** Rails\
**Created:** [May 13, 2008, 1:00am UTC](https://www.ruby-forum.com/t/how-to-create-searchbox/137421 "2008-05-13T01:00:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubynoobie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rubynoobie](https://www.ruby-forum.com/u/rubynoobie)\
**Post date:** [May 13, 2008, 1:00am UTC](https://www.ruby-forum.com/t/how-to-create-searchbox/137421/1 "2008-05-13T01:00:25Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![rubynoobie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rubynoobie](https://www.ruby-forum.com/u/rubynoobie)\
**Post date:** [May 13, 2008, 2:17am UTC](https://www.ruby-forum.com/t/how-to-create-searchbox/137421/2 "2008-05-13T02:17:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rubynoobie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rubynoobie](https://www.ruby-forum.com/u/rubynoobie)\
**Post date:** [May 13, 2008, 2:18am UTC](https://www.ruby-forum.com/t/how-to-create-searchbox/137421/3 "2008-05-13T02:18:53Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![rubynoobie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rubynoobie](https://www.ruby-forum.com/u/rubynoobie)\
**Post date:** [May 13, 2008, 2:29am UTC](https://www.ruby-forum.com/t/how-to-create-searchbox/137421/4 "2008-05-13T02:29:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rubynoobie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rubynoobie](https://www.ruby-forum.com/u/rubynoobie)\
**Post date:** [May 13, 2008, 3:11pm UTC](https://www.ruby-forum.com/t/how-to-create-searchbox/137421/5 "2008-05-13T15:11:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rubynoobie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rubynoobie](https://www.ruby-forum.com/u/rubynoobie)\
**Post date:** [May 13, 2008, 4:37pm UTC](https://www.ruby-forum.com/t/how-to-create-searchbox/137421/6 "2008-05-13T16:37:01Z")

</div>

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 %\>

---

<div class="post-metadata">

**Author:** ![rubynoobie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c2a13f/32.png) [@rubynoobie](https://www.ruby-forum.com/u/rubynoobie)\
**Post date:** [May 13, 2008, 6:28pm UTC](https://www.ruby-forum.com/t/how-to-create-searchbox/137421/7 "2008-05-13T18:28:40Z")

</div>

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
