Using named routes with parameters and form_tag

I am trying to create a simple search form in Rails, but I think something is missing.

I have a named route to search for:

map.search ":first_name/:last_name", :controller => "home", :action => "search" 

I am trying to use this in my search form:

 <% form_tag(search_path, :method => 'get') do %> <%= text_field_tag(:first_name) %> <%= text_field_tag(:last_name) %> <%= submit_tag("Search") %> <% end %> 

But when I load the search form, I get ActionController :: RoutingError:

search_url could not be created from {: action => "search" ,: controller => "home"} - you may have ambiguous routes, or you may need to provide additional parameters for this route. content_url has the following required parameters: [: first_name ,: last_name] - are they all satisfied?

What am I missing? I thought that the fields defined in my form would be automatically associated with the parameters of my route .: - /

Update:

I understand that search_path is generated before the form is displayed now, so it cannot be updated. The obvious in retrospect!

I changed the routes:

 map.search 'search', :controller => "home", :action => "search" map.name ':first_name/:last_name', :controller => "home", :action => "name" 

And now the search action is simple:

 def search redirect_to name_path(params) end 

Everything works! The main goal here was to get this URL on behalf of the route, as a result of the search. Thanks guys!

+6
ruby-on-rails parameters forms
source share
2 answers

form_for generates a form and it should specify all the parameters needed to create the search_path , so it should look like this:

 <% form_tag(search_path, :firstname => 'some_text', :lastname => 'some_text', :method => 'get') do %> 

or at least something like that. The HTML form tag has an action='/some/url' parameter, and so you need to specify all the parameters for search_path . But the above example will not work as you expected.

So what can you do?

  • Create an empty form with action='/' and with js replace it with the contents of the text input fields before submitting.

  • Create another route, for example /search , which returns the parameters from submit and then redirects to the correct path.

Perhaps there are other ways to do this;)

+4
source share

First, search_path is actually a method that accepts an options hash. This method should receive :first_name and :last_name .

Secondly, the browser can send form parameters only as the body of the POST request or as parameters of the query string (for any request method). Thus, unfortunately, there is no way in which the function of sending your own browser files can generate such a URL.

Another way to think about this: what you are doing here fills the action attribute of the form tag with a URL. Rails requires a full URL when creating the form. Thus, all parameters of your route should be specified when calling the form helper, and not on the next POST request.

So, unfortunately, what you are trying to do is not possible in a regular Rails application.

(If you really want to, you can remove it by writing your own form helpers and a bit of Javascript to replace the original browser submission function. Then Javascript will create this URL based on the form fields. I would object to that.)

+2
source share

All Articles