在 routes.rb 中使用自定义 to_param 中的嵌套资源,Strong Parameters 如何允许 created/update 允许?
With Nested Resources in routes.rb with custom to_param, how can Strong Parameters allow created/update to permit?
我找不到可以引导正确方向的内容。其他人关于嵌套资源的类似问题似乎都在 accepts_nested_attributes_for
左右解决了……我不想这样做。我不是要从 parent 中保存 children,而是要直接从 child.
中保存
在我的routes.rb中,我嵌套了我的资源
resources :parents, param: :parent do
resources :children, param: :child
end
parent
和 child
table 都有自己的 id
列,但在 parent
和 [=20= 列上也有唯一索引] 分别,我将在 URL 而不是 id
.
中使用
浏览到每个控制器的 show
、edit
和 index
操作时效果很好。
问题是存在异常保存数据。
我希望问题的 root-cause 不会归结为 child
中的一个字段 table 也被称为 child
因为我就是这样曾经在模型中覆盖 to_param
并且需要保持这种状态。
导航到 编辑 屏幕:http://example.com/parents/harry/children/sally/edit 并在表单上推送提交,returns 这个 NoMethodError异常:
NoMethodError at /parents/harry/children/sally
undefined method `permit' for "sally":String
我确定问题与我的强参数行在 children_controller.rb
中的方式有关。我可以向 require
添加 :parent
和 :child
的散列吗?
def children_params
params.require(:child).permit(:child, :date_of_birth, :nickname)
end
更新1(添加参数):请求参数如下:
{
"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"fAkBvy1pboi8TvwYh8sPDJ6n2wynbHexm/MidHruYos7AqwlKO/09kvBGyWAwbe+sy7+PFAIqKwPouIaE34usg==",
"child"=>"sally",
"commit"=>"Update Child",
"controller"=>"children",
"action"=>"update",
"parent_parent"=>"harry"
}
其他实例变量in-scope 出错时:
@parent
<Parent id: 1, parent: "harry", description: "", user_id: 1, created_at: "2015-06-27 12:00:15", updated_at: "2015-06-27 12:00:15">
@child
<Child id: 1, child: "sally", date_of_birth: nil, parent_id: 1, nickname: nil, created_at: "2015-06-27 12:00:15", updated_at: "2015-06-27 12:00:15">
从 params,您需要像下面这样更改 children_params
def children_params
params.permit(:child, :date_of_birth, :nickname)
end
事实证明,问题 did 似乎是因为模型属性在模型中的名称相同,这也是 params 散列的名称(真正的问题似乎在于)。
我需要做的就是重命名参数哈希。
在children_controller.rb
中,我不得不改变:
def children_params
params.require(:child).permit(:child, :date_of_birth, :nickname)
end
到…
def children_params
params.require(:anything_else).permit(:child, :date_of_birth, :nickname)
end
然后还更改 form_for
我在 new/edit 视图中的表单来自:
<%= simple_form_for([@parent, @child]) do |f| %>
到…
<%= simple_form_for([@parent, @child], as: :child_params) do |f| %>
现在,无论是在测试中还是在用户通过 UI 正常使用网站时,一切都运行良好。
我找不到可以引导正确方向的内容。其他人关于嵌套资源的类似问题似乎都在 accepts_nested_attributes_for
左右解决了……我不想这样做。我不是要从 parent 中保存 children,而是要直接从 child.
在我的routes.rb中,我嵌套了我的资源
resources :parents, param: :parent do
resources :children, param: :child
end
parent
和 child
table 都有自己的 id
列,但在 parent
和 [=20= 列上也有唯一索引] 分别,我将在 URL 而不是 id
.
浏览到每个控制器的 show
、edit
和 index
操作时效果很好。
问题是存在异常保存数据。
我希望问题的 root-cause 不会归结为 child
中的一个字段 table 也被称为 child
因为我就是这样曾经在模型中覆盖 to_param
并且需要保持这种状态。
导航到 编辑 屏幕:http://example.com/parents/harry/children/sally/edit 并在表单上推送提交,returns 这个 NoMethodError异常:
NoMethodError at /parents/harry/children/sally
undefined method `permit' for "sally":String
我确定问题与我的强参数行在 children_controller.rb
中的方式有关。我可以向 require
添加 :parent
和 :child
的散列吗?
def children_params
params.require(:child).permit(:child, :date_of_birth, :nickname)
end
更新1(添加参数):请求参数如下:
{
"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"fAkBvy1pboi8TvwYh8sPDJ6n2wynbHexm/MidHruYos7AqwlKO/09kvBGyWAwbe+sy7+PFAIqKwPouIaE34usg==",
"child"=>"sally",
"commit"=>"Update Child",
"controller"=>"children",
"action"=>"update",
"parent_parent"=>"harry"
}
其他实例变量in-scope 出错时:
@parent
<Parent id: 1, parent: "harry", description: "", user_id: 1, created_at: "2015-06-27 12:00:15", updated_at: "2015-06-27 12:00:15">
@child
<Child id: 1, child: "sally", date_of_birth: nil, parent_id: 1, nickname: nil, created_at: "2015-06-27 12:00:15", updated_at: "2015-06-27 12:00:15">
从 params,您需要像下面这样更改 children_params
def children_params
params.permit(:child, :date_of_birth, :nickname)
end
事实证明,问题 did 似乎是因为模型属性在模型中的名称相同,这也是 params 散列的名称(真正的问题似乎在于)。
我需要做的就是重命名参数哈希。
在children_controller.rb
中,我不得不改变:
def children_params
params.require(:child).permit(:child, :date_of_birth, :nickname)
end
到…
def children_params
params.require(:anything_else).permit(:child, :date_of_birth, :nickname)
end
然后还更改 form_for
我在 new/edit 视图中的表单来自:
<%= simple_form_for([@parent, @child]) do |f| %>
到…
<%= simple_form_for([@parent, @child], as: :child_params) do |f| %>
现在,无论是在测试中还是在用户通过 UI 正常使用网站时,一切都运行良好。