Ruby on Rails: Radio Buttons for Collection Select

Rails - Radio Buttons for collection sets

For radio buttons you have to iterate yourself and output every radio button and its label. It's really easy in fact.

<% RequestType.all.each do |rt| %>
<%= f.radio_button :request_type_id, rt.id %>
<%= f.label :request_type_id, rt.title %>
<% end %>

Or in haml in case it's preferred over erb:

- RequestType.all.each do |rt|
= f.radio_button :request_type_id, rt.id
= f.label :request_type_id, rt.title

How to display checked value for radio button collection

I've figured it out.

It's because I'm using the collection_radio_buttons directly instead of using the form helper object.

From (not storing user value)

<%= collection_radio_buttons(:post, :category_id, Category.all, :id, :name) do |b| %>
<div class="radio">
<%= b.label do %>
<%= b.radio_button + b.text %>
<% end %>
</div>
<% end %>

To (now saving user values)

# Using the form helper `f.collection_radio_buttons` 
# instead of `collection_radio_buttons`.
# Also removed passing the object in as an argument

<%= f.collection_radio_buttons(:category_id, Category.all, :id, :name) do |b| %>
<div class="radio">
<%= b.label do %>
<%= b.radio_button + b.text %>
<% end %>
</div>
<% end %>

I hope this helps anyone in the future.

Thanks

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.

Rails. Splitting a radio button collection into columns with Form Helpers or Simple_form

Coudn't you use a standard rails helper in simple_form form? What about using the radio_button helper directly. Docs

<div class="row prediction">
<div class="col-sm-4">
<%= radio_button 'match_prediction', 'outcome', 'local', checked: @match_prediction.outcome == 'local' %> <%= match.local_team %>
</div>
<div class="col-sm-4">
<%= radio_button 'match_prediction', 'outcome', 'Tie', checked: @match_prediction.outcome == 'Tie' %> <%= 'Tie' %>
</div>
<div class="col-sm-4">
<%= radio_button 'match_prediction', 'outcome', 'visit', checked: @match_prediction.outcome == 'visit' %> <%= match.visiting_team %>
</div>
<%= f.submit 'Save' %>
</div>

And if it is possible to access standard FormHelper in simple_form you can call:

<%= f.radio_button 'outcome', 'xzy' %> <%= 'whatever' %>


Related Topics



Leave a reply



Submit