请帮助我尝试了解这里发生的事情:
我需要批准一个嵌套的片段,但是当我这样做时它说它找不到书。我认为这可能是路由的问题,因为浏览器中的 URL 与 rake 路由不匹配。
如果有人可以握住我的手并像对孩子一样解释这个:)
找不到没有 ID 的书
下面是带有片段#approve 和 before_filter 的控制器。
class SnippetsController < ApplicationController
before_filter :authenticate_user!
before_filter :find_book
def create
#raise params.inspect
@snippet = @book.snippets.create(params[:snippet])
@snippet.user = current_user
if @snippet.save
redirect_to @book
flash[:success] = "Snippet submitted and awaiting approval."
else
flash[:base] = "Someone else has submitted a snippet, please try again later"
redirect_to @book
end
end
def approve
#raise params.inspect
@snippet = @book.snippets.find(params[:id])
@snippet.update_attribute(:approved, true)
redirect_to admins_path
end
def edit
@snippet = @book.snippets.find(params[:id])
end
def update
@snippet = @book.snippets.find(params[:id])
respond_to do |format|
if @snippet.update_attributes(params[:snippet])
format.html { redirect_to @book, notice: 'Comment was successfully updated.' }
else
format.html { render action: "edit" }
end
end
end
private
def find_book
#raise params.inspect
@book = Book.find(params[:book_id])
end
end
现在我明白了,因为我正在写一篇文章,所以我的 rake 路线说明了这一点。
/books/:book_id/snippets/:id(.:format)
这是自定义路线的路线:
active_snippet POST /snippets/:id/activate(.:format)
这是我的书&&片段的自定义路线:批准
post "books/:id/activate" => "books#approve", :as => "active_book"
post "snippets/:id/activate" => "snippets#approve", :as => "active_snippet"
我目前在我的浏览器中有这个 ../snippets/2/activate
呃....不确定我的想法是否正确。