How to change my parameter format?
Here is my Ruby Code:
<%form_tag orders_path, :method => 'get' do%>
From
<%= date_select ("from", "", :start_year => 2010, :order => [:day, :month, :year])%>
To
<%= date_select ("to", "", :start_year => 2010, :order => [:day, :month, :year])%>
<%= submit_tag "Search", :name => nil%>
<%end%>
After I choose some date, I have this result:开发者_JS百科
http://localhost:3000/orders?from[%283i%29]=25&from[%282i%29]=1&from[%281i%29]=2010&to[%283i%29]=25&to[%282i%29]=1&to[%281i%29]=2010
I want the parameter become a easy to read format like:
http://localhost:3000/orders?from=25/01/2010&to=25/01/2010
What should I do?
This is not Rails specific at all, since the format how parameters are passed by HTML-forms is a defined standard. (?input_name=input_value&next_input_name=next_input_value)
. Since you want to have seperate selects for Day/Month/Year i'm afraid there's no other way than using JS before submitting the form.
Nils Riedemann is correct, this is not necessarily a rails problem as much as it is an HTML forms problem.
You may be able to use hidden form fields to do what you want. Create two hidden text fields (one for "from" and one for "to"). Write a Javascript function that takes the values of the month/day/year select fields, builds a string like "25/01/2010" out of their values, and stores this string as the contents of the hidden field. Wire up the Javascript function to run every time one of the date select fields is changed. Now, if your form's 'submit' operation only submits the two hidden fields, you can get a nicer-looking URL like you are wanting.
Alternatively, you may be able to use a URL like:
http://localhost:3000/orders/25-01-2010/25-04-2010
by taking advantage of rails' ability to re-write URLs. See specifically the "Pretty URLs" section.
精彩评论