2014-02-15 90 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>我應該在我的視圖中使用什麼路徑? –