更新 Rails 3 中的多条记录

发布于 2024-11-08 03:15:20 字数 1785 浏览 0 评论 0原文

由于某种原因,当我提交表单时 params[:user] array 未发送(POST):

app/views/groups/edit.html.erb

http //localhost:3000/groups/2

<%= form_for @group do |f| %>
    <% @users.each_with_index do |u, n| %>
      <%= fields_for "users[#{n}]", u do |ff| %>
        <%= ff.select :city_id, grouped_options_for_select(City.states.map {|c| [c.state, City.by_state(c.state).map {|s| [s.name,s.id]}]}, u.city_id) %>
      <% end %>
    <% end %>
  <%= f.submit 'Update Users' %>
<% end %>

输出

  <form accept-charset="UTF-8" action="/groups/2" class="edit_group" id="edit_group_2" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="_method" type="hidden" value="put" /><input name="authenticity_token" type="hidden" value="iT3CGpXpQGxjjAGK9iv2I7x0cGr9d0pW6yc4svzTMFw=" /></div> 
    <select id="users_0_city_id" name="users[0][city_id]">
      <optgroup label="State1">
        <option value="1">City1</option> 
        <option value="2">City2</option> 
      </optgroup>
      <optgroup label="State2">
        <option value="3">City3</option> 
        <option value="4">City4</option> 
      </optgroup>
    </select>
  </form>

发送参数

http://localhost:3000/groups/2/edit

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"iT3CGpXpQGxjjAGK9iv2I7x0cGr9d0pW6yc4svzTMFw=", "commit"=>"Update Users", "id"=>"2"}

非常感谢任何帮助

For some reason when I submit the form params[:user] array is not send (POST):

app/views/groups/edit.html.erb

http://localhost:3000/groups/2

<%= form_for @group do |f| %>
    <% @users.each_with_index do |u, n| %>
      <%= fields_for "users[#{n}]", u do |ff| %>
        <%= ff.select :city_id, grouped_options_for_select(City.states.map {|c| [c.state, City.by_state(c.state).map {|s| [s.name,s.id]}]}, u.city_id) %>
      <% end %>
    <% end %>
  <%= f.submit 'Update Users' %>
<% end %>

output

  <form accept-charset="UTF-8" action="/groups/2" class="edit_group" id="edit_group_2" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="_method" type="hidden" value="put" /><input name="authenticity_token" type="hidden" value="iT3CGpXpQGxjjAGK9iv2I7x0cGr9d0pW6yc4svzTMFw=" /></div> 
    <select id="users_0_city_id" name="users[0][city_id]">
      <optgroup label="State1">
        <option value="1">City1</option> 
        <option value="2">City2</option> 
      </optgroup>
      <optgroup label="State2">
        <option value="3">City3</option> 
        <option value="4">City4</option> 
      </optgroup>
    </select>
  </form>

sent parameters

http://localhost:3000/groups/2/edit

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"iT3CGpXpQGxjjAGK9iv2I7x0cGr9d0pW6yc4svzTMFw=", "commit"=>"Update Users", "id"=>"2"}

Any help very appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

维持三分热 2024-11-15 03:15:20

在您看来:

<%= form_for @group do |f| %>
   <%= fields_for "users[#{n}]", u do |builder| %>
   <%= builder.select :city_id, grouped_options_for_select(City.states.map {|c| [c.state, City.by_state(c.state).map {|s| [s.name,s.id]}]}, u.city_id) %>
   <% end %>
   <%= f.submit 'Update Users' %>
<% end %>

在您的组模型中:

has_many :users
accepts_nested_attributes_for :users

在您的控制器中,请确保构建用户,否则他们将不会显示。

def new
   @group = Group.new
   users = User.all #obviously chose your own conditions
   @group.users = users
end 

In your view:

<%= form_for @group do |f| %>
   <%= fields_for "users[#{n}]", u do |builder| %>
   <%= builder.select :city_id, grouped_options_for_select(City.states.map {|c| [c.state, City.by_state(c.state).map {|s| [s.name,s.id]}]}, u.city_id) %>
   <% end %>
   <%= f.submit 'Update Users' %>
<% end %>

In your Group model:

has_many :users
accepts_nested_attributes_for :users

In your controller make sure you build the users or they won't show up.

def new
   @group = Group.new
   users = User.all #obviously chose your own conditions
   @group.users = users
end 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文