I have two models that can be commented on, Books and Movies.
The comments are votable
In my routes file:
resources :books, :path => '' do
resources :comments do
member do
post :vote_up
end
end
In my comments controller:
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
In my view:
<%= link_to('vote for this post!', vote_up_book_comment_path(comment),
:method => :post) %>
I keep on getting this error:
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]
This is the gem I am using for the voting: https://github.com/bouchard/thumbs_up
The comments can belong to either the books or movies, how do I set this up in the routes? Also, how do I set up the votes in the routes? (all the comments are votable)