Converting a Checkbox example to Radio Buttons

People,

I have successfully worked my way through this exercise:

   https://www.sitepoint.com/save-multiple-checkbox-values-database-rails

(but changing "professors" to "tippers" and "expertises" to "tips" - for a football tipping app).

Then the only change I made was to: app/views/tippers/_form.html.erb

from:

<div class="field">
   <%= f.label "Tip" %><br />
   <%= f.collection_check_boxes :tip_ids, Tip.all, :id, :teams do |b| %>
     <div class="collection-check-box">
       <%= b.check_box %>
       <%= b.label %>
     </div>
   <% end %>
</div>

to:

   <div class="field">
     <%= f.label "Tip" %><br />
     <%= f.collection_radio_buttons( :tip_ids, Tip.all, :id, :teams ) do

b> %>

       <div class="collection-radio-button">
         <%= b.label + b.radio_button %>
       </div>
     <% end %>
   </div>

which actually works in the view but unlike the Checkbox version the data is not saved in the tippers_tips table.

What am I missing?

Thanks,

Phil.

The line above looks a bit iffy to me, but the main question is: what
do you see in your logs? any error messages?

If not use logging statements (or pry) to inspect the params being
returned.

Hassan,

        <%= b.label + b.radio_button %>

which actually works in the view but unlike the Checkbox version the data is
not saved in the tippers_tips table.

The line above looks a bit iffy to me, but the main question is: what
do you see in your logs? any error messages?

If not use logging statements (or pry) to inspect the params being
returned.

Thanks for responding - I see in the log when I edit a Tipper:

Processing by TippersController#update as HTML
   Parameters: {"utf8"=>"✓", "authenticity_token"=>"AhTjh08BAh6NALIeQ5yc7qVvjO8S6rnBaCQlsocM6ffiVkKMo2eKaAmP/QBA2Iiu79naCW+Pd8XZsTMbnYbutQ==", "tipper"=>{"fname"=>"Jocelyn", "tip_ids"=>"1"}, "commit"=>"Update Tipper", "id"=>"3"}
   Tipper Load (0.1ms) SELECT "tippers".* FROM "tippers" WHERE "tippers"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]]
Unpermitted parameter: tip_ids

During the tables generation I did:

   rails g migration CreateJoinTableTipTipper tip tipper

and the only two mentions of "tip_ids" is in app/views/tippers/_form.html.erb :

   <div class="field">
     <%= f.label "Tip" %><br />
     <%= f.collection_radio_buttons( :tip_ids, Tip.all, :id, :teams ) do

b> %>

       <div class="collection-radio-button">
         <%= b.radio_button %>
         <%= b.label %>
       </div>
     <% end %>
   </div>

and app/controllers/tippers_controller.rb :

   params.require(:tipper).permit(:fname, tip_ids:[])

I deleted "[]" since there should be only one tip id returned but that caused and error . .

Thanks,

Phil.

Rename the tip_ids to tip_id. A radio button only returns one value, and tip_ids means you are expecting an array back, as you would get from a group of checkboxes. Check your strong parameters (permit) clause in your controller -- make sure that it is set to accept a single scalar value for that form input (singular). Also check that the relationship in your models is not one to many, but one to one (has_one/belongs_to). Changing the type of input you want to use has ramifications for your entire application, not just the view.

Walter