Question about Rails 5 and collection_radio_buttons
.
How do you have it display the checked value of a radio input when you try and edit the form?
<%= 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 %>
With select box f.collection_select
it remembers the value but not with collection_radio_buttons
My routes file is as follows
resources :posts, as: :entries
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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments