2011-06-14 38 views
2

試圖摧毀一個「貢獻」一個用戶的投票時,我收到以下錯誤:沒有路由匹配{:動作=>「消滅」

No route matches {:action=>"destroy", :controller=>"contribution_votes", 
        :id=>#<ContributionVote contribution_id: 8245, user_id: 40>} 


但不知道爲什麼。這裏是按鈕發送請求

<%= button_to "undo voteup!", 
     contribution.contribution_votes.find_by_user_id(current_user), 
     :method => :delete, :class => 'contribution_vote undo' %> 


這裏是在控制器中的「毀滅」行動:

def destroy 
    @vote = current_user.contribution_votes.find(params[:id]) 
    @vote.destroy 


    @contribution = Contribution.find_by_id(@vote.contribution_id) 


    @contribution_decrement = @contribution.decrement(:votes) 

if @vote.destroy and @contribution_decrement.save 
    respond_to do |format| 
    format.html {redirect_to :back} 
    format.js 
end 
else 
     redirect_to :back   

end 
end 


(這裏有些冗餘,我知道,但它是在以填寫稍後的日期)

而這裏的設置在routes.rb

resources :contribution_votes, :only => [:create, :destroy] 

任何人都可以幫忙嗎?我懷疑答案是顯而易見的,但我無法在任何地方找到它。順便說一句

<%= button_to "undo voteup!", 
    contribution_votes_path(current_user.id), 
    :method => :delete, :class => 'contribution_vote undo' %> 

..::

+0

你能告訴我們該按鈕生成的HTML嗎? – Nerian 2011-06-14 16:09:01

+0

@John - 我對rails非常陌生,所以不知道如何去補救(我也不知道爲什麼我使用的是嵌套資源,但會查找它)。有什麼方法可以明確設置控制器嗎?爲什麼它不會尋找正確的控制器?謝謝你的幫助。 – jonic 2011-06-14 16:47:59

+0

您在更改路由後是否重新啓動了服務器? – Arsen7 2011-06-14 19:31:23

回答

1

鑑於更改您的代碼(我認爲這將有助於)在您的控制檯類型rake routes,並驗證您使用權的路線路徑(我可以錯) 。

+0

這似乎不工作 - 可能是因爲控制器試圖找到id = user.id的contribution_vote。表單需要同時傳遞current_user.id和contribution.id來標識要刪除的權利...... – jonic 2011-06-14 16:26:30

+0

路由似乎也很好:'contribution_vote DELETE /contribution_votes(.:format){:controller =>「contribution_votes」 ,:action =>「destroy」}' – jonic 2011-06-14 16:29:20

+0

但是,無論如何bor1s! – jonic 2011-06-14 16:29:54

相關問題