简体   繁体   English

在route.rb中使用自定义to_param的嵌套资源时,如何允许强参数创建/更新以允许?

[英]With Nested Resources in routes.rb with custom to_param, how can Strong Parameters allow created/update to permit?

I can't find something that'll lead in the right direction. 我找不到能指引正确方向的东西。 Everyone else's similar issues with nested resources seems to resolve around accepts_nested_attributes_for … which I'm not trying to do. 嵌套资源中的每个其他accepts_nested_attributes_for似的问题似乎都可以在accepts_nested_attributes_for …附近解决,而我并不想这样做。 I'm not trying to save children from the parent, I'm trying to save directly from the child. 我不是要从父母那里拯救孩子,而是要直接从孩子那里拯救孩子。

In my routes.rb, I have nested my resource 在我的routes.rb中,我嵌套了资源

resources :parents, param: :parent do
  resources :children, param: :child
end

parent and child tables both have their own id column, but also have unique index on columns parent and child respectively, which I was to be used in the URL instead of the id . parent表和child表都有自己的id列,但分别在parentchild列上具有唯一索引,我将在URL中使用它而不是id

http://example.com/parents/parent/children/child http://example.com/parents/parent/children/child

This is working fine browsing around going to the show , edit and index actions of each controller. 这样可以很好地浏览每个控制器的showeditindex动作。

The problem is there are exceptions saving data. 问题是存在保存数据的异常。

I'm hoping the root-cause of the issue doesn't come down to a field in the child table is also called child as that's what I've used to override to_param in the model and need to keep it that way. 我希望问题的根本原因不会归结为child表中的字段也称为child因为这就是我在模型中重写to_param所需要的,并且需要保持这种方式。

Navigating to the edit screen: http://example.com/parents/harry/children/sally/edit and pushing submit on the form, returns this NoMethodError exception: 导航到编辑屏幕: http : //example.com/parents/harry/children/sally/edit并在表单上推送Submit ,返回此NoMethodError异常:

NoMethodError at /parents/harry/children/sally 在/ parents / harry / children / sally处出现NoMethodError
undefined method `permit' for "sally":String 未定义的方法“ permit”,用于“ sally”:字符串

I'm sure the problem is something to do with how my Strong Parameters line is in children_controller.rb . 我确定问题是与children_controller.rb强参数”行有关。 Can I add to require a hash of :parent and :child maybe? 我可以添加require :parent:child的哈希吗?

def children_params
  params.require(:child).permit(:child, :date_of_birth, :nickname)
end

Update 1 (Added params): Here are the request parameters: 更新1(添加的参数):这是请求参数:

{
  "utf8"=>"✓", 
  "_method"=>"patch", 
  "authenticity_token"=>"fAkBvy1pboi8TvwYh8sPDJ6n2wynbHexm/MidHruYos7AqwlKO/09kvBGyWAwbe+sy7+PFAIqKwPouIaE34usg==", 
  "child"=>"sally", 
  "commit"=>"Update Child", 
  "controller"=>"children", 
  "action"=>"update", 
  "parent_parent"=>"harry"
}

Other instance variable in-scope at time of error: 发生错误时其他实例变量作用域内:

@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">

From the params , you need to change the children_params like below params中 ,您需要像下面这样更改children_params

def children_params
  params.permit(:child, :date_of_birth, :nickname) 
end

Turns out, the problem did seem to be because the a model attribute is named the same at the model, which is what the params hash is also called (where the real problem seemed to lie). 事实证明,问题似乎确实是因为模型属性在模型上被命名为相同的属性,这也称为params哈希(真正的问题似乎在这里)。

All I needed to do was rename the params hash. 我要做的就是重命名params哈希。

In children_controller.rb , I had to change: children_controller.rb ,我必须更改:

def children_params
  params.require(:child).permit(:child, :date_of_birth, :nickname)
end

to… 至…

def children_params
  params.require(:anything_else).permit(:child, :date_of_birth, :nickname)
end

and then also change the form_for my form in the new/edit views from: 然后从以下位置在new / edit视图中更改form_for我的表单:

<%= simple_form_for([@parent, @child]) do |f| %>

to… 至…

<%= simple_form_for([@parent, @child], as: :child_params) do |f| %>

Now, all works well across the board, in both tests and when a user uses the site via the UI as normal. 现在,无论是在测试中还是在用户正常通过UI使用网站时,所有工具都可以正常运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM