Get an array from a Rails form

后端 未结 5 1251
野趣味
野趣味 2021-01-12 13:10

I need to design a form for a account resource. In that form, i need to collect some set of ids as an array in the params hash in attribute called

5条回答
  •  长发绾君心
    2021-01-12 14:00

    I found this to be the cleanest way...

    If you are working with straight data and want to send back an array without using any of these @objects:

    <%= form_for :team do |t| %>
      <%= t.fields_for 'people[]', [] do |p| %>
        First Name: <%= p.text_field :first_name %>
        Last Name: <%= p.text_field :last_name %>
      <% end %>
    <% end %>
    

    your params data should return like this:

    "team" => {
      "people" => [
        {"first_name" => "Michael", "last_name" => "Jordan"},
        {"first_name" => "Steve", "last_name" => "Jobs"},
        {"first_name" => "Barack", "last_name" => "Obama"}
      ]
    }
    

提交回复
热议问题