更新 Rails 3 中的多条记录
由于某种原因,当我提交表单时 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="✓" /><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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您看来:
在您的组模型中:
在您的控制器中,请确保构建用户,否则他们将不会显示。
In your view:
In your Group model:
In your controller make sure you build the users or they won't show up.