在嵌套资源控制器中嵌套资源的情况下,有没有办法识别父资源名称?
resources :parent do
resources :child
end
class ChildController
def update
request.parent_resource //this should return :parent (resource name)
end
end
提前致谢。
答案 0 :(得分:0)
逻辑上,如果Parent has_many Children,您只能使用嵌套资源。然后一切都落到了位置。
## models
class Parent < ApplicationRecord
has_many :children
end
class Child < ApplicationRecord
belongs_to :parent
end
## routes.rb
resources :parents do
resources :children
end
## controllers
class ChildrenController
def update
@child = Child.find(params[:id])
parent = @child.parent
end
end