3 深度嵌套形式
我想知道在控制器上处理 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
I wonder what is the best way to deal with 3 levels deep and deeper of nested forms on controller
An Example:
Shop Controller
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 Accepts_nested_attributes_for 变得难以使用,那么您的构建方法实际上是您唯一的其他选择,而无需对代码进行更深入的重构。
If accepts_nested_attributes_for is getting unwieldy, your build methods are really your only other option without doing a deeper refactor of your code.
我建议您阅读 ActiveRecord 的
accepts_nested_attributes_for
方法。在大多数情况下,它将消除对所有
build_...(...)
方法的需要,并且可以与嵌套表单完美配合。I recommend you read up on ActiveRecord's
accepts_nested_attributes_for
method. It will eliminate the need for all of yourbuild_...(...)
methods in most all cases, and works beautifully with nested forms.