Forum: Ruby on Rails Pass paramter via form

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0de83fd56f1af530034ba8efa5490b1b?d=identicon&s=25 John Butler (Guest)
on 2006-04-05 13:21
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 %>
  <p align =center><%= text_field 'search', 'search',"size" =>"20" %>
  <%= submit_tag 'Search'  %></p>
<%= 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.
6b4aa22626f1183813b502e2d1f4ea2a?d=identicon&s=25 Juanjo Bazan (Guest)
on 2006-04-05 13:38
(Received via mailing list)
Use the params hash to retrieve parameter values:
@posts = Post.search_posts(params[:search])

HTH
Juanjo
0de83fd56f1af530034ba8efa5490b1b?d=identicon&s=25 John Butler (Guest)
on 2006-04-05 13:49
I have just done that but i am still getting no search results.

<%= start_form_tag :action => 'search', :args => search %>
  <p align =center><%= text_field 'search' , 'search', "size" =>"20" %>
  <%= submit_tag 'Search'  %></p>
<%= 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?
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2006-04-05 14:25
(Received via mailing list)
> 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/
0de83fd56f1af530034ba8efa5490b1b?d=identicon&s=25 John Butler (Guest)
on 2006-04-05 14:36
>
> 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.
0de83fd56f1af530034ba8efa5490b1b?d=identicon&s=25 John Butler (Guest)
on 2006-04-05 14:55
: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}%'",
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2006-04-05 14:55
(Received via mailing list)
<..>
>
> 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/
0de83fd56f1af530034ba8efa5490b1b?d=identicon&s=25 John Butler (Guest)
on 2006-04-05 15:05
Thanks for your help!
4daf0b71d5d9a3882e583c0e72eaf5dc?d=identicon&s=25 Alan Francis (Guest)
on 2006-04-05 15:09
John Butler 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
This topic is locked and can not be replied to.