Question

i currently do have a form for a event-signup, with unlimited amount greater one of participants in signup-form, so you can append forms via javascript, which i copied form the fields_for generator of the first must-be participant.

Now i have a gender-field, which is just called :gender, and like 10 other inputs for other personal data.

the genderfield however is the only one which is not send for each and every participiant. the field is just missing.

the fields_for helper named it for example:

event[participants][][email]
event[participants][][gender]

when i add 3 participants, i get an array like:

[{email: email1, gender: male}, {email:email2}, {email:email3}]

set to rails.

since a radio-button is grouped by its name, i thought i might add the the index to the empty brackets, to have it like

event[participants][1][gender]

for the 2nd participant.

this leads to

ERROR TypeError: expected Array (got Rack::Utils::KeySpaceConstrainedParams) for param `participants'

in Webrick.

I am a bit confused of how to continue, giving all the inputs the index maybe, which feels not like rails-way at all.

Any suggestions how to get this to work? my aim is to to get the gender sent for each participant.

Best regards and thanks, daniel

the code, simplified:

the form:

= form_for @signup do |f|
    - participant_count = @signup.participants.count
    - @signup.participants.each_with_index do |participant,index|
      = f.fields_for "participants[]", participant do |p|
      = render :partial=>'participant_form', |
        :locals=>{:p=>p, :participant=>participant, :index=>index, :participant_count => participant_count}

the _participant-partial, for one input that works and the gender:

%table
  %tr
   %th
    Gender / Only sent for the first participant:(
    %span *
  %td
    =p.radio_button :gender, 'male'
    %span  Male
    =p.radio_button :gender, 'female'
    %span Female

%tr
  %th
    Name
    %span *
  %td
    = p.text_field :first_name / this works just fine! for EACH participant.

results in this html code:

 <input id="event_participants__first_name" name="event[participants][][first_name]" size="30" type="text" value="" />
  and
 <input id="event_participants__gender_male" name="event[participants][][gender]" style="float:left; margin-right: 20px;" type="radio" value="male">
  for each participant.
Was it helpful?

Solution

okay, since radiobuttons need to be grouped by their names - which is the sense of them - i had to hack my way around here.

in my solution, i just added some non-array-related indexed radio-buttons which triggerd a javascript function which sets the value of the proper, array`d hidden field which was the radio buttons before.

works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top