我想知道在控制器
上处理嵌套表单的3层深层和深层的最佳方法是什么示例:
商店控制器
def new
@user = current_user
@shop = @user.build_shop
@type = @shop.build_type
end
def create
@user = current_user
@shop = @user.build_shop(params[:shop])
@type = shop.build_type(params[:type])
if @shop.save
flash.now[:success] = "blah"
render :show
else
render :new
end
end
答案 0 :(得分:2)
如果accepts_nested_attributes_for变得笨拙,那么在不对代码进行更深层次的重构的情况下,您的构建方法实际上是您唯一的选择。
答案 1 :(得分:1)
我建议您阅读ActiveRecord的accepts_nested_attributes_for
方法。在大多数情况下,它将消除对所有build_...(...)
方法的需要,并且可以与嵌套表单一起使用。