2012-11-05 32 views
1

我目前确实有一个事件注册的表单,无限量的注册表单中的参与者数量更多,因此您可以通过javascript添加表单,我将其复制到fields_for生成器中第一个必须参与者。fields_for单选按钮在rails 3.2

现在我有一个性别字段,这就是所谓的性别,和其他个人数据的其他输入一样。

然而性别字段是唯一不会针对每个参与者发送的字段。 该字段只是缺少。

把它命名为例如fields_for帮手:

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

当我添加3名参与者,我得到这样一个数组:

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

设置为轨道。

由于单选按钮是由它的名称分组,我想我可能会在索引添加到空括号,把它像

event[participants][1][gender] 

的第二个参与者。

这导致

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

的WEBrick。

我对如何继续,给所有的输入索引也许有点困惑,这感觉不像rails-way。

任何建议如何让这个工作?我的目标是为每位参与者发送性别信息。

此致和感谢, 丹尼尔

代码,简化了:

形式:

= 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} 

的_participant偏,对于一个输入,工程和性别:

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

结果在此HTML代码中:

<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. 
+0

你应该分享你正在使用创建这个表单的代码。 – sevenseacat

+0

这样做;)谢谢。 – thedanielhanke

回答

0

没关系,因为单选按钮需要用自己的名字来分组 - 这是他们的感觉 - 我不得不砍在这里我的方式。

在我的解决方案

,我只是增加了一些非阵列相关的索引的单选按钮,其triggerd一个JavaScript功能,其设置适当的值,array`d隐藏字段这是之前的单选按钮。

作品。

+0

我有同样的问题。当:( – Jimmy