2014-02-15 87 views
0

我有两种模型可以评论,书籍和电影。评论和投票路线

的意见是可投票

在我的路线文件:

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

的意见可以属于任何书籍或电影,我怎么在路线设置呢?另外,如何在路线中设置投票? (所有的评论都是可投票)

回答

2

如果运行rake routes,你可能会得到在读取这样的输出线:

vote_up_book_comment POST /:book_id/comments/:id/vote_up(.:format) comments#vote_up 

要特别注意这部分 - 它告诉你什么vote_up_book_comment_path方法期望作为参数:

/:book_id/comments/:id/vote_up(.:format) 

而且,你的错误信息是给你一些提示:

No route matches ... 
:id=>nil, :book_id=>#<Comment id: 3 ... 
missing required keys: [:id] 

路径帮助程序需要一个id(用于注释)和book_id,并且它们所需的顺序显示在rake routes(book_id first,然后id)中。

因此,总之,你需要传递一个bookvote_up_book_comment_path

<%= link_to('vote for this post!', vote_up_book_comment_path(@book, comment), :method => :post) %> 
+0

现在我得到这个错误:的SQLite3 :: ConstraintException:votes.voteable_id不能为null:INSERT INTO “票”( “created_at”, “的updated_at” )VALUES(?,?) –

+1

根据[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]'? –

+0

谢谢soooooooooooo多!您的评论系统完美运作。事实证明,thumbs_up是一个废话,我结束了使用acts_as_votable宝石,现在一切都像一个魅力! :) 再次感谢! :) –

1

由于您使用的是你的路由member,铁轨需要在URL中的ID。在vote_up_book_comment_path(comment)中,您提供了book id,但没有comment id。它将comment的观点解释为一本书。要解决此问题,请包含一个书本对象,将vote_up_book_comment_path(comment)更改为vote_up_book_comment_path(@book, comment)。在您的控制器的new方法中,还包括book变量,以便您的视图模板可以访问它。

要建立在任何书本或电影评论:窝他们

因为评论是从书本或视频分开,你不想在书。相反,如果您的评论是单独的路线,并且只有在您登录时才会嵌套在书籍或视频下。这样,您可以在视图模板中使用隐藏字段来存储它是书籍还是视频,然后将其传递给控制器​​。现在控制器有必要的信息来知道它是否是book_idmovie_id

它看起来像这样的代码:

resources :books do 
    resources :comments, only: [:new, :edit] 
end 

你会为你想要的所有资源做到这一点。最后征求意见,你会做这样的事情:

resources :comments, except: [:new, :edit] 
+0

我该怎么做? –

+0

这是否解决了您的问题? – remydib

+0

现在我得到了:未定义的方法'vote_up_book_comment_path'为#<#:0x00000101c90e08>我应该在我的视图中使用什么路径? –