Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an object in a radio button tag?

For my form I am using my Product model:

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

On my form I have the :purchase_date working correctly when I create multiple products but also want to make the radio_button_tag 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 didn't work for me, my database doesn't flag as either false or true.

I think the problem lies in the params of the "send_to_data". Unlike the "purchase_date" it isn't finding the 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 a way to map it to the object like the purchase date does?

like image 981
LearningRoR Avatar asked Dec 11 '11 03:12

LearningRoR


People also ask

How do I make radio buttons Unselectable in HTML?

Answer: To make a radio button not selectable, in the button's INPUT tag you can use an onclick event handler like this: <INPUT type="radio" name="myButton" value="theValue" onclick="this. checked=false; alert('Sorry, this option is not available!')

Is radio button single selection?

Radio buttons allow a user to select a single option among multiple options. You can set the Choice Value of each option, for each button, as well as group these buttons by giving them the same Group Name. Radio buttons have Default styling.

Are radio buttons selected or checked?

About the radio buttons component Since only one radio button can be selected at a time (within the same group), each available choice must be its own item and label. In contrast, checkboxes may show a single label, with the checked/unchecked status of the item meaning opposite things.


2 Answers

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

(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.

This mean for ActiveRecord-based form you need to use use FormHelper (and its radio_button helper method).

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 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!

like image 176
Aliaksei Kliuchnikau Avatar answered Oct 20 '22 00:10

Aliaksei Kliuchnikau


check this page form_helpers first, here, the right way should be

<%= radio_button_tag(:send_to_data, 1) %>
<%= radio_button_tag(:send_to_data, 0) %>
like image 40
Chaoyu Avatar answered Oct 20 '22 01:10

Chaoyu