Radio Buttons on Rails

How to use radio button correctly in rails?

see label(object_name, method, content_or_options = nil, options = nil, &block)

  <div class="field">
<%= f.label :autolyse %><br />
<%= f.label :autolyse, "Yes", value: "true" %>
<%= f.radio_button :autolyse, true %>
<%= f.label :autolyse, "No", value: "false" %>
<%= f.radio_button :autolyse, false, checked: true %>
</div>

How do you link radio buttons with text fields in rails?

You can achieve this by using jquery. You just need to write onChange function for the text field and in that function you can set the last radio input to checked
Considering your view:

<div class="form-group">
<%= f.label :Owner %><br>
<%= f.radio_button :owner, "Option1" %>
<%= f.label :owner, "Option1" %><br>
<%= f.radio_button :owner, "Option2" %>
<%= f.label :owner, "Option2" %><br>
<%= f.radio_button :owner, value: "", :class => "custom" %>
<%= f.text_field :owner, :class => "user-defined" %>
</div>

The jquery function for this would be:

$(".user-defined").change(function(){
if($(this).val()){
$(".custom").prop('checked', true);
}
else{
$(".custom").prop('checked', false);
}
});

This is not tested but i hope it gives you the right direction.

Rails multiple radio buttons

I'd go with enum instead of separate columns, this was it's way easier and logically more correct: e.g

enum pricing_option: { no_price: 0, fixed_amount: 1, per_hour: 2 }

In views:

<%= collection_radio_buttons(:foo, :pricing_option, Foo.pricing_options, :first, :first)%>

Multiple radio buttons getting selected in Rails app

There are two options:

  1. Using JavaScript on the client-side to uncheck the radio buttons.
  2. Using radio buttons with the same name. It this case you will have to change the way you pass the :is_answer parameter and manually assign the value in options_attributes.

Method 1 details:

See this question radio different names - only check one

Method 2 details:

Instead of passing :is_answer parameter for each option you can pass a single parameter for the question having chosen answer id as the value. Lets name it "answer_id". We want this parameter to be in the params[question]
hash in the controller, so the whole name will be "question[answer_id]". Although radio buttons are generated for each option, only the chosen one will be sent to the server as they all have the same name.

    <%= form.fields_for :options, question.options.each do |a| %>
<div class="field">
<%= a.label :options %>
<%= a.text_area :body %>
<%= radio_button_tag "question[answer_id]", a.object.id, a.object.is_answer? %>
<%= a.check_box :_destroy %>
<%= a.label :_destroy, 'delete' %>
</div>
<% end %>

https://apidock.com/rails/v4.2.7/ActionView/Helpers/FormTagHelper/radio_button_tag

In the controller you will have to manually assign the option's is_answer parameter based on the answer_id value.

def question_params
result = params.require(:question).permit(:body, :answer_id, options_attributes: [:id, :body, :question_id])

answer_id = result.delete(:answer_id)

result[:options_attributes].values.each do |option_attrs|
option_attrs[:is_answer] = option_attrs[:question_id] == answer_id
end

result
end

If you need further details please let me know. I will update the answer to give more information.

Explain how the Radio button works for form_for?

Try this .......

<div class="form-group">
<%= f.label :gender %> <br />
<%= f.label :gender_male, 'male' %>
<%= f.radio_button :gender, 'male', :checked => true %>
<%= f.label :gender_female, 'female' %>
<%= f.radio_button :gender, 'female' %><br />
</div>

Hope this will help you.



Related Topics



Leave a reply



Submit