【发布时间】:2012-02-07 02:09:01
【问题描述】:
我有两个模型的嵌套路由,如下所示:
resources :books do
resources :chapters, :shallow => true
end
并且希望能够为一本书的章节提供嵌套表单。
如何检查每个批量分配的章节书是否属于用户。
我试图像这样将它添加到控制器中
class ChaptersController < ApplicationController
before_filter :check_ownership_of_chapters_book
def check_ownership_of_chapters_book
if Book.find(params[:book_id]).author != current_user
flash[:error] = "You are not the author of the book you are adding the chapter to"
redirect_to root_url
end
end
...
它会产生这个错误
“找不到没有 ID 的书”
因为书的 id 没有在参数中传递。
但我在想也许我应该在模型中进行检查(检查插入章节的书是否属于 current_user)
【问题讨论】:
标签: ruby-on-rails validation nested-forms