【发布时间】:2012-05-30 12:15:22
【问题描述】:
我使用 rails 3.2 并且我想防止批量分配。我有亲子关系。
class Parent < ActiveRecord:Base
has_many :children
attr_accessible :name
end
class Child < ActiveRecord:Base
belongs_to :parent
attr_accessible :title
end
在我的 routes.rb 中,子资源没有嵌套在父资源中。现在我有一个链接可以用new_child_path(@parent.id) 创建一个新的孩子。这将我引导至localhost:3000/child/new?parent_id=1,我最终以new 操作:
def new
@child = Child.new
@parent = Parent.find(params[:parent_id])
@child.parent = @parent
end
我的问题是:如何为子实体写我的_form.html.erb?我不能将f.hidden_field 用于parent_id,因为在我的创建操作中,它会因为批量分配而中断。另一方面,当我救孩子时,我需要通过parent_id 来了解我的父母。我还没有找到一个很好的工作示例。
【问题讨论】:
标签: ruby-on-rails mass-assignment