Rails字段用于从text_field接收数组

时间:2012-12-20 20:44:43

标签: ruby-on-rails arrays forms

我有一个form_for和一个嵌套的fields_for

现在看起来像这样

form_for blah 
  fields_for :badgetype do |b|
    b.text_field :name
    b.text_field :keyword

Params看起来像badgetype: { :name => "foo", :keyword => "bar" }

我想要的是设置其中的5个,然后得到这样的东西:

badgetype: { 1 => { :name => "foo", :keyword => "bar" }, 
             2 => { :name => "boof", :keyword => "baz" },
             ... 
           }

我将如何做到这一点?

谢谢

1 个答案:

答案 0 :(得分:-1)

<%= form_for blah do |a| %>
  <% (1..5).each do |number| %>
    <%= a.fields_for "badgetype[#{number}]" do |b| %>
      <%= b.text_field :name %>
      <%= b.text_field :keyword %>
    <% end %>
  <% end %>
  <%= a.submit %>
<% end %>