我有一个form_for,它将fields_ in Chands_ for for party and Guest模型(聚会有很多客人,接受宾客的嵌套属性),并且客人属于聚会。我有一个form_ for我的聚会模型,它列出了聚会中每个客人的某些领域。我的表格工作正常,但我想简单地列出表格中每个客人的名称,因此看起来:
"Bob"
attending: yes || no
meal choice: chicken || veggie || pasta
"Susie"
attending: yes || no
meal choice: chicken || veggie || pasta
现在,我所拥有的只是:
attending: yes || no
meal choice: chicken || veggie || pasta
attending: yes || no
meal choice: chicken || veggie || pasta
我如何仅列出guest.first_name for fields_for的每个迭代???显然,我的Guests_form.first_name不起作用。
我表格的代码是:
您的聚会中的人:
<%= form_for @party do |f| %>
<%= f.hidden_field :party_id, value: current_party.id %>
<%= f.fields_for @guests do |guests_form| %>
<%= guests_form.first_name %>
<%= guests_form.label :attending, "attending?" %>
<%= guests_form.radio_button :attending, true, :checked => true %>
<%= guests_form.label :attending, "yes!", :value => true %>
<%= guests_form.radio_button :attending, false, :checked => false %>
<%= guests_form.label :attending, "no", :value => false %>
<%=guests_form.label :meal_id, "meal choice" %>
<%=guests_form.radio_button :meal_id, :checked => 1 %>
<%= guests_form.label :meal_id, "chicken", :value => 1 %>
<%=guests_form.radio_button :meal_id, :checked => 2 %>
<%= guests_form.label :meal_id, "veggie", :value => 2 %>
<%=guests_form.radio_button :meal_id, :checked => 3 %>
<%= guests_form.label :meal_id, "pasta", :value => 3 %>
<%end%>
<%= f.submit "submit", class: 'btn btn-primary btn-lg' %>
<%end%>
我以前从未使用过fields_for,因此任何帮助都将不胜感激!
您正在设计吗?如果是,则:
<% if user_signed_in? %>
<p>Hello <%= current_user.first_name %>, let us know your preferences: </p>
<%form_for @party do...
...
...
<% end %>
<% end %>
您需要在隐藏的字段中捕获User_id,以便将其与选择一起保存。我会考虑使用Challocy的复选框或收音机按钮。