我有兩種模型可以評論,書籍和電影。評論和投票路線
的意見是可投票
在我的路線文件:
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寶石,現在一切都像一個魅力! :) 再次感謝! :) –