【发布时间】:2014-03-15 03:22:51
【问题描述】:
我有两个可以评论的模型,书籍和电影。
cmets 是可投票的
在我的路线文件中:
resources :books, :path => '' do
resources :comments do
member do
post :vote_up
end
end
在我的 cmets 控制器中:
class CommentsController < ApplicationController
def create
book.comments.create(new_comment_params) do |comment|
comment.user = current_user
end
redirect_to book_path(book)
end
private
def new_comment_params
params.require(:comment).permit(:body)
end
def book
@book = Book.find(params[:book_id])
end
def vote_up
begin
current_user.vote_for(@comment = Comment.find(params[:id]))
render :nothing => true, :status => 200
rescue ActiveRecord::RecordInvalid
render :nothing => true, :status => 404
end
end
end
在我看来:
<%= link_to('vote for this post!', vote_up_book_comment_path(comment),
:method => :post) %>
我不断收到此错误:
No route matches {:action=>"vote_up", :controller=>"comments", :id=>nil, :book_id=>#<Comment id:
3, body: "fantastic read!", book_id: 113, created_at: "2014-02-15 17:08:10", updated_at:
"2014-02-15 17:08:10", user_id: 8>, :format=>nil} missing required keys: [:id]
这是我用于投票的 gem:https://github.com/bouchard/thumbs_up
cmets 可以属于书籍或电影,如何在路线中设置?另外,如何在路线中设置投票? (所有的 cmets 都可以投票)
【问题讨论】:
标签: ruby-on-rails ruby routes nested-routes