Pass paramter via form


#1

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.


#2

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

HTH
Juanjo


#3

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?


#4

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.


#5

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

:wink:

Regards,
Rimantas

http://rimantas.com/


#6

: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}%’”,


#7

Thanks for your help!


#8

<…>

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/


#9

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