How to pass an object in a switch tag?

For my form, I use my Product model:

 class Product < ActiveRecord::Base attr_accessible :purchase_date, :send_to_data end 

In my form, I have :purchase_date , which works correctly when I create several products, but also want radio_button_tag to do the same:

 <%= 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 %> 

This did not work for me, my database is not marked as false or true.

I think the problem is with the "send_to_data" parameters. Unlike "purchase_date" it does not find an object (product).

 {"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"=>"", "exact_url"=>""}, "2"=>{"product_name"=>"", "price"=>"", "product_store"=>"", "exact_url"=>""}, "3"=>{"product_name"=>"", "price"=>"", "product_store"=>"", "exact_url"=>""}, "4"=>{"product_name"=>"", "price"=>"", "product_store"=>"", "exact_url"=>""}}, "commit"=>"Done"} 

Is there any way to match it with an object similar to the date of purchase?

+7
source share
2 answers

As far as I can see, you are confusing FormHelper and FormTagHelper . You use the form tag helper, which, according to the documentation

(FormTagHelper) provides a number of methods for creating form tags that do not rely on an Active Record object assigned to a template, such as FormHelper.

This means that for an ActiveRecord-based form, you need to use the FormHelper method (and its helper method radio_button ).

Code with radio_button_tag form tag helper

 <%= radio_button_tag(:send_to_data, 1) %> <%= radio_button_tag(:send_to_data, 0) %> 

generates the following 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" /> 

And the code with radio_button form helper

 <%= radio_button("product", :send_to_data, 1) %> <%= radio_button("product", :send_to_data, 0) %> 

generates:

  <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" /> 

Hope this helps!

+8
source

check this form_helpers page, here the correct path should be

 <%= radio_button_tag(:send_to_data, 1) %> <%= radio_button_tag(:send_to_data, 0) %> 
+3
source

All Articles