I’ve got a Rails project which has a car model and it has multiple pictures. I’ve created a nested form so I can input the cars details and pictures all at the same time. I would like a radio button to select the picture to display in the gallery.
%table
= simple_form_for [@car] :html => {:multipart => true} do |f|
= f.error_messages
= f.input :make
= f.input :model
= f.input :engine
= f.input :price
= f.simple_fields_for :car_pictures do |builder|
.picture
= builder.input :caption
= builder.input :picture
.thumb= image_tag builder.object.picture(:thumb)
= f.button :submit
Is it possible to use a radio button in the ‘fields_for’ section to select the default/main picture? Or is there a better way?
Update
By putting the following under the fields_for all the radio buttons work independently, which is not what I want.
= builder.radio_button(:favourite, "test")
Each radio button is getting a unique name.
Update
What if I used the object_id as the value, how would I go about linking this in the create method?
= f.radio_button(:favourite_pic, builder.object_id)
Yes, your last guess was correct. I made a test application in which the customer can select a favorite photo: https://github.com/eicca/attrs_test (see commit changes marked by “added favorite example”).
There are two main points (according to your situation):
and
And you need to add
favorite_pic_idfield to yourcarstable.