simple_form with radio buttons for nested attributes

1,982 views
Skip to first unread message

Giannicola Olivadoti

unread,
Feb 22, 2011, 7:48:46 AM2/22/11
to SimpleForm
Hello guys, I have a problem with your wonderful gem simple_form.

I have these two models:

# id :integer(4) not null, primary key
# category :string(255)
# content :text
class Question < ActiveRecord::Base
has_many :choices
accepts_nested_attributes_for :choices
end

# id :integer(4) not null, primary key
# content :text
# correct :boolean(1)
# question_id :integer(4)
class Choice < ActiveRecord::Base
belongs_to :question
end

I have to create the form for creating questions: this form will have
the fields for inserting the content of the question and the content
of the 3 possible choices, with a radio button for every choice in
order to select the correct answer between the 3 choices.

This is the form:

<%= simple_form_for @question do |question_form| %>
<%= question_form.error_notification %>

<div class="inputs">
<%= question_form.input :content %>
<%= question_form.input :category, :collection =>
get_categories, :include_blank => false %>

<%= question_form.fields_for :choices do |choice_form| %>
<%= choice_form.input :content %>
<%= radio_button_tag("question[choices_attributes][][correct]",
"false") %>
<% end %>
</div>

<div class="actions">
<%= question_form.button :submit %>
</div>
<% end %>

The problem of this code is that produces input radio with
name="question[choices_attributes][][correct]" for all the three
choices: infact when you submit the values the system log gives a
"TypeError (expected Array (got Hash) for param `choices_attributes')"

How can I insert the index in question[choices_attributes][?index?]
[correct] ?

I hope you can understand my problem.
I'm looking forward to your reply!

PS: You've done a great job with this gem!
Message has been deleted

Carlos Antonio da Silva

unread,
Feb 23, 2011, 7:38:08 AM2/23/11
to plataformate...@googlegroups.com, Giannicola Olivadoti
Sorry, I didn't have time to look at your issue.

What I can see in stack overflow that is not in your post here, is that you're going through each choice to render the fields_for:

    <% @question.choices.each do |choice| %>

This is wrong when working with nested attributes imo. If you just build the coices before, and do sth like that:

    3.times { @question.choices.build } # controller

    <%= question_form.fields_for :choices do |choice_fields| %>

Remember you can use simple_fields_for as well.
Also, you can use simple form collection_radio to help you:

    <%= question_form.association :choices, :as => :radio %>

Please give it a try.

On Wed, Feb 23, 2011 at 9:28 AM, Giannicola Olivadoti <olin...@gmail.com> wrote:
I also posted this question in stackoverflow:
http://stackoverflow.com/questions/5090820/rails-radio-button-selection-for-nested-objects

On Feb 22, 9:15 pm, Giannicola Olivadoti <olinic...@gmail.com> wrote:
> I mean... can I use simple_form to do this job or I have to stick with
> the radio_button or the radio_button_tag methods of Rails? Even in the
> latter case, I don't know how to do it...
>
> Thanks for helping
>
> Giannicola



--
At.
Carlos A. da Silva

Giannicola Olivadoti

unread,
Feb 23, 2011, 7:58:19 AM2/23/11
to SimpleForm
Hello Carlos, thank you for your response!

The problem with this radio button is that I have to set the correct
attribute of just one choice to true. I can also use a select button
that makes me select the correct answer, but how can I deal with the
params array?

I try and try and try.... but this message is making me sick:
TypeError (expected Array (got Hash) for param `choices_attributes').

On Feb 23, 1:38 pm, Carlos Antonio da Silva
<carlosantoniodasi...@gmail.com> wrote:
> Sorry, I didn't have time to look at your issue.
>
> What I can see in stack overflow that is not in your post here, is that
> you're going through each choice to render the fields_for:
>
>     <% @question.choices.each do |choice| %>
>
> This is wrong when working with nested attributes imo. If you just build the
> coices before, and do sth like that:
>
>     3.times { @question.choices.build } # controller
>
>     <%= question_form.fields_for :choices do |choice_fields| %>
>
> Remember you can use simple_fields_for as well.
> Also, you can use simple form collection_radio to help you:
>
>     <%= question_form.association :choices, :as => :radio %>
>
> Please give it a try.
>
> On Wed, Feb 23, 2011 at 9:28 AM, Giannicola Olivadoti
> <olinic...@gmail.com>wrote:
>
>
>
> > I also posted this question in stackoverflow:
>
> >http://stackoverflow.com/questions/5090820/rails-radio-button-selecti...

Giannicola Olivadoti

unread,
Feb 23, 2011, 7:28:59 AM2/23/11
to SimpleForm
I also posted this question in stackoverflow:
http://stackoverflow.com/questions/5090820/rails-radio-button-selection-for-nested-objects

On Feb 22, 9:15 pm, Giannicola Olivadoti <olinic...@gmail.com> wrote:
> I mean... can I use simple_form to do this job or I have to stick with
> the radio_button or the radio_button_tag methods of Rails? Even in the
> latter case, I don't know how to do it...
>
> Thanks for helping
>
> Giannicola
>
> On 22 Feb, 13:48, Giannicola Olivadoti <olinic...@gmail.com> wrote:
>

Carlos Antonio da Silva

unread,
Feb 24, 2011, 8:52:26 PM2/24/11
to plataformate...@googlegroups.com, Giannicola Olivadoti
Wich the solution I recommended, you should be able to give a :selected option to f.association.
Reply all
Reply to author
Forward
0 new messages