Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Search form problem
7 Posts
Search form problem


I'm creating a search form on my site with the help of the rails cast tutorial (http://railscasts.com/episodes/37-simple-search-form) and this tutorial (https://we.riseup.net/rails/simple-search-tutorial)

But now I'm having some problems when submitting the search form. (see pastie)

I'm just getting an blank page without anything on it. The url changes, this is added search?method=get

That's all, I don't know what the problem could be, maybe someone will see it.


There must be something wrong with your routes settings so wee need content of your "routes.rb"

I've updated my pastie.

The problem isn't in your routes, it's in your arguments to form_tag, you have this:

form_tag :controller => 'nederlands', :action => 'search', :method => 'get'

But the API docs give the method signature as this:

form_tag(url_for_options = {}, options = {}, *parameters_for_url, &block)

What Ruby does when you just pass in hash elements as parameters is slurps them all up into the first hash argument that it can. So, what you're doing is akin to this:

form_tag( { :controller => 'nederlands', :action => 'search', :method => 'get' }, nil, nil)

Ie. you're passing the :controller, :action and :method hash elements in as the first argument to form_tag, ie. as the url_for_options. So Rails passes those to url_for and that builds your URL for you, the :method param is added to the end of the URL as a name/value pair.

What you actually wanted to do was to pass the :method hash element in the second argument to form_tag. Hopefully that's all you needed to point you in the right direction.

So I've changed my code to

'nederlands', :action => 'search', :method => 'get' }, nil, nil) %>

                  <p> nil %> </p>

But I'm getting an expected end error. Am I doing something wrong?

Hmm, ok, I thought I'd given you enough to work this out. Your problem this time through is that you've removed the 'do' from the end of your form_tag line (that's why it's complaining about the missing end) - but more importantly that you've just copied that example with the nils shows me that you really don't know what you're doing here. What you want for that line is this:

 'nederlands', :action => 'search' }, { :method => 'get' } ) do %>

See how we're now passing two arguments to form_tag, first the url_for_options parameter with the controller and action to form the URL that the form will be submitted to, and then the options parameter where we put the method that the form will be submitted with.

Hi Jason,

Thanks for your answer, I must admit that I've tried adding a do at the end, but that wasn't working because the method was put to next to the action.

It's working fine, thanks for that:)

7 Posts
Login to add your message