Reputation: 515
I have radiobuttons creaated in loop.
- @annual_packages.each do |plan|
= f.radio_button('plan_id', plan.id)
= plan.title
How can I make condition to check radio if plan.id == @plan.id
Doesn't work:
= f.radio_button('plan_id', plan.id, checked: (plan.id == @plan.id))
Loop code:
= form_for @organization, url: subscription_create_path, remote: true, method: :post do |f|
- unless @annual_packages.blank?
- @annual_packages.each do |plan|
= f.radio_button('plan_id', plan.id)
= plan.title
Upvotes: 0
Views: 965
Reputation: 718
If you are using simple_form
or form_for
, and f is your form object then you can simply do:
for form_for:
radio_button(object_name, method, tag_value, options = {})
refer
in your case:
- @annual_packages.each do |plan|
radio_button("your object name", "plan_id", plan.id, options = {})
If the current value of method(object.plan_id) is tag_value(plan) the radio button will be checked.
for simple_form:
= f.input :plan_id, as: :radio_buttons, collection: @annual_packages
Upvotes: 0
Reputation: 643
Can you post your form_for
code?
Cause if your column plan_id
have any value, it should be checked automatically if it matches the tag_value`.
If the current value of method is tag_value the radio button will be checked.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-radio_button
Upvotes: 1