对于我的表单,我使用的是我的产品型号:
class Product < ActiveRecord::Base attr_accessible :purchase_date,:send_to_data end
在我的表单上,我有:purchase_date在我创建多个产品时正常工作,但也想让radio_button_tag做同样的事情:
<%= form_tag create_multiple_products_path,:method => :post do %> <%= date_select("product","purchase_date") %> <%= radio_button_tag(:send_to_data,1) %> <%= radio_button_tag(:send_to_data,0) %> <% @products.each_with_index do |product,index| %> <%= fields_for "products[#{index}]",product do |up| %> <%= render "fields",:f => up %> <% end %> <% end %> <%= submit_tag "Done" %> <% end %>
我认为问题在于“send_to_data”的参数.与“purchase_date”不同,它没有找到对象(产品).
{"product"=>{"purchase_date(2i)"=>"12","purchase_date(3i)"=>"11","purchase_date(1i)"=>"2011"},"send_to_data"=>"1","products"=>{"0"=>{"product_name"=>"Test","price"=>"23","product_store"=>"13","exact_url"=>""},"1"=>{"product_name"=>"","price"=>"","product_store"=>"","2"=>{"product_name"=>"","3"=>{"product_name"=>"","4"=>{"product_name"=>"","exact_url"=>""}},"commit"=>"Done"}
有没有办法将它映射到对象,如购买日期呢?
解决方法
据我所知,你混淆了
FormHelper和
FormTagHelper.
根据文档,您使用Form标签助手
根据文档,您使用Form标签助手
(FormTagHelper) provides a number of methods for creating form tags that doesn’t rely
on an Active Record object assigned to the template like FormHelper does.
这意味着您需要使用基于ActiveRecord的表单使用FormHelper(及其radio_button帮助方法).
使用radio_button_tag表单标记帮助程序的代码
<%= radio_button_tag(:send_to_data,1) %> <%= radio_button_tag(:send_to_data,0) %>
生成以下HTML:
<input id="send_to_data_1" name="send_to_data" type="radio" value="1" /> <input id="send_to_data_0" name="send_to_data" type="radio" value="0" />
和代码radio_button形成帮手
<%= radio_button("product",:send_to_data,1) %> <%= radio_button("product",0) %>
产生:
<input id="product_send_to_data_1" name="product[send_to_data]" type="radio" value="1" /> <input id="product_send_to_data_0" name="product[send_to_data]" type="radio" value="0" />
希望这可以帮助!