2014-11-14 48 views
0

我有一個模型「東西」,其中每一個has_many「評論」,每一個has_many「投票」。我希望能夠投票評論關於事情顯示頁面。這是我到目前爲止有:Rails 4:'redirect_to current_thing'undefined error

評論控制器:

def votecomment 
    @comment = Comment.find(params[:id]) 
    Vote.create!(voteable_id: params[:id], voteable_type: 'Comment') 
    redirect_to current_thing 
end 

事物的看法:

<%= link_to 「Vote」, vote_comment_path(:id => comment.id), method: :post %> 

路線:

post 'comments/:id/vote' => 'comments#vote', as: 'vote_comment' 

但我又回到這個錯誤:

NameError in CommentsController#votecomment 
undefined local variable or method `current_thing' for #<CommentsController:0x007f98efa69c00> 

我試着將方法移到東西控制器,但我得到了完全相同類型的錯誤。

我在做什麼錯?

+2

嘗試'redirect_to @ comment.thing' – Santhosh 2014-11-14 12:22:52

+0

你是怎麼想到'current_thing'的? – Santhosh 2014-11-14 12:24:29

+0

你可以在你的控制器上發佈你的'current_thing'的代碼嗎? – 2014-11-14 12:26:01

回答

2

假設你有comment.rb以下關係

belongs_to :thing 

您可以訪問使用@comment.thing評論的事物對象。由於redirect_to接受對象,你可以做

redirect_to @comment.thing 
1

如果您熟悉設計,並且您看到ex current_user,那麼您必須明白沒有任何東西被稱爲current_thing這是gem中的方法,而不是您創建的每個模型的填充方法。

所以,如果你想這樣的事情add方法您application_controller甚至應用助手讓current_thing

def current_thing 
    Thing.find() --> or whatever the way you get that current thing. 
end