Message from discussion
simple_form with radio buttons for nested attributes
Received: by 10.142.157.12 with SMTP id f12mr730239wfe.36.1298464210032;
Wed, 23 Feb 2011 04:30:10 -0800 (PST)
X-BeenThere: plataformatec-simpleform@googlegroups.com
Received: by 10.143.178.9 with SMTP id f9ls728195wfp.0.p; Wed, 23 Feb 2011
04:30:09 -0800 (PST)
MIME-Version: 1.0
Received: by 10.142.142.15 with SMTP id p15mr217366wfd.0.1298464139391; Wed,
23 Feb 2011 04:28:59 -0800 (PST)
Received: by j9g2000prj.googlegroups.com with HTTP; Wed, 23 Feb 2011 04:28:59
-0800 (PST)
Date: Wed, 23 Feb 2011 04:28:59 -0800 (PST)
In-Reply-To: <57b8f22f-430b-4f19-b783-a5c1a3c6b17d@n11g2000vbm.googlegroups.com>
X-IP: 93.64.39.18
References: <5a7b1c50-2bd2-4f06-9d5d-eacc5bfa0668@t13g2000vbo.googlegroups.com>
<57b8f22f-430b-4f19-b783-a5c1a3c6b17d@n11g2000vbm.googlegroups.com>
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-GB;
rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13,gzip(gfe)
Message-ID: <9a12b7b5-3541-4a31-b7d7-7bea956267cb@j9g2000prj.googlegroups.com>
Subject: Re: simple_form with radio buttons for nested attributes
From: Giannicola Olivadoti <olinic...@gmail.com>
To: SimpleForm <plataformatec-simpleform@googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
I also posted this question in stackoverflow:
http://stackoverflow.com/questions/5090820/rails-radio-button-selection-for=
-nested-objects
On Feb 22, 9:15=A0pm, 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:
>
> > Hello guys, I have a problem with your wonderful gem simple_form.
>
> > I have these two models:
>
> > # =A0id =A0 =A0 =A0 =A0 =A0 =A0 =A0:integer(4) =A0 =A0 =A0not null, pri=
mary key
> > # =A0category =A0 :string(255)
> > # =A0content =A0 =A0 :text
> > class Question < ActiveRecord::Base
> > =A0 =A0 has_many :choices
> > =A0 =A0 accepts_nested_attributes_for :choices
> > end
>
> > # =A0id =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 :integer(4) =A0 =A0 =A0not =
null, primary key
> > # =A0content =A0 =A0 =A0 =A0 =A0:text
> > # =A0correct =A0 =A0 =A0 =A0 =A0 :boolean(1)
> > # =A0question_id =A0 :integer(4)
> > class Choice < ActiveRecord::Base
> > =A0 =A0 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:
>
> > <%=3D simple_form_for @question do |question_form| %>
> > =A0 <%=3D question_form.error_notification %>
>
> > =A0 <div class=3D"inputs">
> > =A0 =A0 =A0 =A0 <%=3D question_form.input :content %>
> > =A0 =A0 <%=3D question_form.input :category, :collection =3D>
> > get_categories, :include_blank =3D> false %>
>
> > =A0 =A0 =A0 =A0 <%=3D question_form.fields_for :choices do |choice_form=
| %>
> > =A0 =A0 =A0 =A0 =A0 <%=3D choice_form.input :content %>
> > =A0 =A0 =A0 =A0 =A0 <%=3D radio_button_tag("question[choices_attributes=
][][correct]",
> > "false") %>
> > =A0 =A0 =A0 =A0 <% end %>
> > =A0 </div>
>
> > =A0 <div class=3D"actions">
> > =A0 =A0 <%=3D question_form.button :submit %>
> > =A0 </div>
> > <% end %>
>
> > The problem of this code is that produces input radio with
> > name=3D"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!
>