我有两种模型可以评论,书籍和电影。评论和投票路线
的意见是可投票
在我的路线文件:
resources :books, :path => '' do
resources :comments do
member do
post :vote_up
end
end
在我的意见控制器:
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) %>
我一直在得到这个呃ROR:
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]
这是我使用的投票宝石:https://github.com/bouchard/thumbs_up
的意见可以属于任何书籍或电影,我怎么在路线设置呢?另外,如何在路线中设置投票? (所有的评论都是可投票)
现在我得到这个错误:的SQLite3 :: ConstraintException:votes.voteable_id不能为null:INSERT INTO “票”( “created_at”, “的updated_at” )VALUES(?,?) –
根据[vote_for'的thumbs_up源代码](https://github.com/bouchard/thumbs_up/blob/master/lib/acts_as_voter.rb#L69),我猜测这意味着'Comment.find(params [:id])'在'CommentsController#vote_up'内返回'nil' ...是按照预期进入控制器的'params [:id]'? –
谢谢soooooooooooo多!您的评论系统完美运作。事实证明,thumbs_up是一个废话,我结束了使用acts_as_votable宝石,现在一切都像一个魅力! :) 再次感谢! :) –