Rails 表单中的多个对象 [英] Multiple objects in a Rails form
本文介绍了Rails 表单中的多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想以一种形式编辑模特照片的多个项目.我不确定如何用表单正确地呈现和发布它,以及如何在控制器的更新操作中收集项目.
I want to edit multiple items of my model photo in one form. I am unsure of how to correctly present and POST this with a form, as well as how to gather the items in the update action in the controller.
这就是我想要的:
<form>
<input name="photos[1][title]" value="Photo with id 1" />
<input name="photos[2][title]" value="Photo with id 2" />
<input name="photos[3][title]" value="Custom title" />
</form>
参数只是一个例子,就像我上面所说的:我不确定以这种形式发布这些值的最佳方式.
The parameters are just an example, like I stated above: I am not sure of the best way to POST these values in this form.
在控制器中我想要这样的东西:
In the controller I want to something like this:
@photos = Photo.find( params[photos] )
@photos.each do |photo|
photo.update_attributes!(params[:photos][photo] )
end
推荐答案
在 Rails 4 中,就这样
In Rails 4, just this
<%= form_tag photos_update_path do %>
<% @photos.each do |photo| %>
<%= fields_for "photos[]", photo do |pf| %>
<%= pf.text_field :caption %>
... other photo fields
这篇关于Rails 表单中的多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文