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!
ruby-on-rails parameters forms
Stewart johnson
source share