How do I get a nicer params hash returned by my non-model form?
I am trying to set up a non-model form to generate a report based on search queries and am struggling with the syntax of the form helpers. Here's a simplified version (one of many attempts) that demonstrates my problem:
<%= form_tag url_for(:action => "show") do %>
<%= label_tag :procedure %><br />
<%= select_tag :procedure, options_for_select(['A','B','C']) %><br />
<%= label_tag :procedure_date %><br />
<%= date_select :report, :procedure_date %><br />
<%= submit_tag :Submit %>
<% end %>
When this form is submitted, the params hash that results looks like this;
{"utf8"=>"✓", "authenticity_token"=>"bQ3ILfHtp0qod/cGNr4LDAWzwLdyHMBQ6xyO7+DLXns=",
"procedure"=>"A", "report"=>{"procedure_date(1i)"=>"2011", "procedure_date(2i)"=>"4",
"procedure_date(3i)"=>"7"}, "commit"=>"Submit", "controller"=>"reports", "action"=>"show"}
What I want (I think) is something more like this;
{"utf8"=>"✓", "authenticity_token"=>"bQ3ILfHtp0qod/cGNr4LDAWzwLdyHMB开发者_开发知识库Q6xyO7+DLXns=",
"report"=>{"procedure"=>"A", "procedure_date(1i)"=>"2011", "procedure_date(2i)"=>"4",
"procedure_date(3i)"=>"7"}, "commit"=>"Submit", "controller"=>"reports", "action"=>"show"}
I could then access the variables I needed with params[:report][:procedure]
and params[:report][:procedure_date]
.
I have no idea how to set the form up to achieve this.
Additionally, as it is I am unable to access the date that is returned at present with params[:report][:procedure_date]. Do I have to process this in the controller to assemble the date from the components? Surely there's a Rails way to take care of this (it certainly isn't a problem when using a model based form)?
you need set html option :name for selet_tag
<%= form_tag url_for(:action => "show") do %>
<%= label_tag :procedure %><br />
<%= select_tag :procedure, options_for_select(['A','B','C']), {:name => "report[report_procedure]"} %><br />
<%= label_tag :procedure_date %><br />
<%= date_select :report, :procedure_date %><br />
<%= submit_tag :Submit %>
<% end %>
精彩评论