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]

任何人都可以帮忙吗?我怀疑答案很明显,但我在 SO 上的任何地方都找不到。

4

1 回答 1

1

将您的代码更改为(我认为这会有所帮助):

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

..顺便说一句:rake routes在您的控制台中输入并验证您是否使用正确的路线路径(我可能会弄错)。

于 2011-06-14T16:09:48.013 回答