# Pass paramter via form

**URL:** <https://www.ruby-forum.com/t/pass-paramter-via-form/93642>\
**Category:** Rails\
**Created:** [April 5, 2006, 1:21pm UTC](https://www.ruby-forum.com/t/pass-paramter-via-form/93642 "2006-04-05T13:21:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 5, 2006, 1:21pm UTC](https://www.ruby-forum.com/t/pass-paramter-via-form/93642/1 "2006-04-05T13:21:43Z")

</div>

Hi,

I want to allow users ti search my blog. I have the following code  
below to initiate the form.

\<%= start\_form\_tag :action =\> ‘search’, :search =\> search %\>

\<%= text\_field 'search', 'search',"size" =\>"20" %\> \<%= submit\_tag 'Search' %\>

\<%= end\_form\_tag %\>

And below i have the def in the blog controller

def search  
@posts = Post.search\_posts(:search)  
@categories = Category.find(:all, :order =\> “name asc”)  
@archive = Post.month\_posts(4)  
end

I cant seem to pass the value in the search textbox to the def search in  
the blog controller. The SQL query on the post model works fine when i  
input arguments myself. Code for post model below.

def self.search\_posts(args)  
find(:all,  
:conditions =\> “title LIKE ‘%{args}%’ OR body LIKE ‘%{args}%’”,  
:order =\> “created\_at desc”)  
end

Can anyone tell me where i am going wrong? I keep getting 0 search  
results. I think the problem is passing the value from the form to the  
controller.

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 5, 2006, 1:38pm UTC](https://www.ruby-forum.com/t/pass-paramter-via-form/93642/2 "2006-04-05T13:38:40Z")

</div>

Use the params hash to retrieve parameter values:  
@posts = Post.search\_posts(params[:search])

HTH  
Juanjo

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 5, 2006, 1:49pm UTC](https://www.ruby-forum.com/t/pass-paramter-via-form/93642/3 "2006-04-05T13:49:31Z")

</div>

I have just done that but i am still getting no search results.

\<%= start\_form\_tag :action =\> ‘search’, :args =\> search %\>

\<%= text\_field 'search' , 'search', "size" =\>"20" %\> \<%= submit\_tag 'Search' %\>

\<%= end\_form\_tag %\>

blog controller

def search  
@posts = Post.search\_posts(params[:args])  
@categories = Category.find(:all, :order =\> “name asc”)  
@archive = Post.month\_posts(4)  
end

post model  
def self.search\_posts(args)  
find(:all,  
:conditions =\> “title LIKE ‘%{args}%’ OR body LIKE ‘%{args}%’”,  
:order =\> “created\_at desc”)  
end

I am using eclipse with the rad rails plug-in, is there anyway to step  
through the code like so i can see the value of what is being passed?

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 5, 2006, 2:36pm UTC](https://www.ruby-forum.com/t/pass-paramter-via-form/93642/4 "2006-04-05T14:36:37Z")

</div>

> Can you spot what is different from what Juanjo suggested:
> 
> @posts = Post.search\_posts(params[:search])

I’ve changed the :search to :args in the controller aswell as the view  
so it shouldn’t make any difference should it? :search is just the name  
of the paramater i am passing.

i changed it back to :search and it still didn’t work. 0 search  
results.

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 5, 2006, 2:25pm UTC](https://www.ruby-forum.com/t/pass-paramter-via-form/93642/5 "2006-04-05T14:25:00Z")

</div>

> I have just done that but i am still getting no search results.  
> \<…\>  
> @posts = Post.search\_posts(params[:args])  
> \<…\>

Can you spot what is different from what Juanjo suggested:

@posts = Post.search\_posts(params[:search])

😉

## Regards, Rimantas

[http://rimantas.com/](http://rimantas.com/)

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 5, 2006, 2:55pm UTC](https://www.ruby-forum.com/t/pass-paramter-via-form/93642/6 "2006-04-05T14:55:20Z")

</div>

:search doesn’t work it must the paramter must have to be called  
something different than the def and the field i’m passing.

The problem was in the model. I forgot to put the # before the args!

:conditions =\> “title LIKE ‘%#{args}%’ OR body LIKE ‘%#{args}%’”,

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 5, 2006, 3:05pm UTC](https://www.ruby-forum.com/t/pass-paramter-via-form/93642/7 "2006-04-05T15:05:33Z")

</div>

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 5, 2006, 2:55pm UTC](https://www.ruby-forum.com/t/pass-paramter-via-form/93642/8 "2006-04-05T14:55:35Z")

</div>

\<…\>

> I’ve changed the :search to :args in the controller aswell as the view  
> so it shouldn’t make any difference should it? :search is just the name  
> of the paramater i am passing.

Ok. You use text\_field, which expects model name, so you justs put  
search here.  
In this case you can access its value with params[:search][:search]

So you’d have  
\<%= text\_field ‘search’, ‘search’,“size” =\>“20” %\> in form and  
@posts = Post.search\_posts(params[:search][:search]) in controller

Another possible way is to use text\_field\_tag insted of text\_field.  
In this case params[:search] is OK. Then you have:

In form: \<%= text\_field\_tag ‘search’,nil, :size =\> 20 %\> (you might want  
to replace nil with params[:search])

and

@posts = Post.search\_posts(params[:search]) - controller

## Regards, Rimantas

[http://rimantas.com/](http://rimantas.com/)

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 5, 2006, 3:09pm UTC](https://www.ruby-forum.com/t/pass-paramter-via-form/93642/9 "2006-04-05T15:09:00Z")

</div>

John B. wrote:

> \<%= start\_form\_tag :action =\> ‘search’, :search =\> search %\>

The :search =\> search thing isn’t relevant.

> \<%= text\_field ‘search’, ‘search’,“size” =\>“20” %\>

The two args here ‘foo’ and ‘bar’ need accessed in the controller as

params[:foo][:bar]

so you’ll need [:search][:search]

The first arg is for grouping. e.g you could have

‘search’, ‘text’  
‘search’, ‘category’

that would generate a params hash like this:

{search =\> { text =\> ‘…’, :category =\> ‘…’} }

You could then get the whole thing by params[:search] or a particular  
sub-part like paramns[:search][:category]

HTH,

Alan
