我有两个模特
thing.rb
has_one :subthing
subthing.rb
belongs_to :thing和我的路由
resources :thing do
resource :subthing
end
resources :subthing但是,我在控制器上的显示方法
def show
@subthing = Subthing.find(params[:id])
end当我访问
http://example.org/things/1/subthing给了我一个错误
没有ID就找不到Subthing
我觉得这个应该由框架来处理.也就是说,应该计算出相关的子事物是属于事物的。
我是遗漏了什么,还是不能在这里使用相同的控制器方法来处理Subthings本身和Subthings (当它们是事物的一部分)。
还是需要显式地告诉每个潜在关联的控制器。即
def show
if params[:thing_id].present?
@subthing = @thing.find(params[:thing_id]).subthing
else
@subthing = Subthing.find(params[:id])
end
end发布于 2016-03-03 19:36:38
如果您打算对嵌套资源和顶级资源同时使用相同的SubthingsController,则需要按建议进行如下操作:
def show
if params[:thing_id].present?
@subthing = @thing.find(params[:thing_id]).subthing
else
@subthing = Subthing.find(params[:id])
end
end但是你的控制器变得很复杂而且不值得。您最好重新定义您的路线或使用两个单独的控制器。
https://stackoverflow.com/questions/35780327
复制相似问题