Reputation: 5314
Is it possible to generate the url for the form submit based off of the values in the form? I am trying to create a search form that asks for the make and model
<% form_tag make_model_search_path(:client_id => @client.id) do %>
<%= select_tag :make, options_for_select(make_list) %>
<%= select_tag :model, options_for_select(model_list) %>
<%= submit_tag "Search" %>
<% end %>
The route I want to use is the following to display the results
map.make_model_search "client/:client_id/tags/search/make/:make/model/:model", :controller => "tags", :action => "make_model_search", :method => :get
But since the URL is generated before any parameters are sent I do not know how to do this
The reason I want it to be done this way is because I need to add pagination to this eventually by appending "page/:page" to this route.
If this is not possible. I also do not understand why the params are not in the the url as
"?client_id=ID&make=MAKE&model=MODEL"
It it my understanding that this will allow me to do the same thing
Thank YOU!
Upvotes: 1
Views: 2218
Reputation: 2640
Using the "client/:client_id/tags/search/" is mostly the REST approach that Rails uses for working with your routes. In that view, you are accessing representations of resources (say, a client).
The "?client_id=ID" way is just sending some params.
So, in the first case, you are explicitly asking for a representation of a client, while on the second one you are never sure what's going on. It's a simpler way of expressing and requesting information.
Now, in your question, I think you are talking about the second case. If you do want to append the params using a GET statement (since the form will default to POST), you can add a method param:
form_tag make_model_search_path(:client_id => @client.id), :method => "get"
As a side note, I think you are over complicating your route. If you just want to use params, you could just work with something like
"client/:client_id/search/", :controller => "tags", :action => "make_model_search", :method => :get
Your params (:model, :make) will be available in your controllers and if you want to use pagination (checkout the will_paginate plugin) it will work fine.
Good luck!
BTW, the routes are not actually generated before. The "/:client_id/", "/:make" and others act as placeholders that the route engine will use to check if some route responds to a typed URL and execute it after assigning the variables according.
Upvotes: 1
Reputation: 17790
I also do not understand why the params are not in the the url as
"?client_id=ID&make=MAKE&model=MODEL"
It it my understanding that this will allow me to do the same thing
From the documentation for form_tag
:
The method for the form defaults to POST.
You need to add :method => "get" to your form_tag
options if you want to have the options passed in the query string.
Upvotes: 3