Converting a Checkbox example to Radio Buttons

15 views
Skip to first unread message

Philip Rhoades

unread,
Mar 1, 2018, 11:11:16 AM3/1/18
to rubyonra...@googlegroups.com
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.
--
Philip Rhoades

PO Box 896
Cowra NSW 2794
Australia
E-mail: ph...@pricom.com.au

Hassan Schroeder

unread,
Mar 1, 2018, 12:21:41 PM3/1/18
to rubyonrails-talk
On Thu, Mar 1, 2018 at 8:10 AM, Philip Rhoades <ph...@pricom.com.au> wrote:

> <%= 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.

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Philip Rhoades

unread,
Mar 1, 2018, 9:19:33 PM3/1/18
to rubyonra...@googlegroups.com, Hassan Schroeder
Hassan,


On 2018-03-02 04:21, Hassan Schroeder wrote:
> On Thu, Mar 1, 2018 at 8:10 AM, Philip Rhoades <ph...@pricom.com.au>
> wrote:
>
>> <%= 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 . .

Walter Lee Davis

unread,
Mar 1, 2018, 10:44:58 PM3/1/18
to rubyonra...@googlegroups.com
> Philip Rhodes

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
Reply all
Reply to author
Forward
0 new messages