0

我的控制器中的操作有问题 - 当我在视图中调用 link_to 并使用销毁操作时出现错误:

ActionController::UrlGenerationError

No route matches {:controller=>"user_votes", :action=>"destroy", :recipient_uid=>2911238}

路线:

App::Application.routes.draw do

  devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
  resources :users
  resources :votes 
  resources :user_votes
  root 'main#home'

end

用户投票控制器:

class UserVotesController < ApplicationController

    def destroy
        @user_vote = UserVote.find_by(params[:recipient_uid])
        @user_vote.destroy
        redirect_to root_url
    end

end

查看文件:

 = link_to 'Delete', {controller: "user_votes", action: "destroy", recipient_uid: friend.uid}, method: "delete"

你能告诉我我在哪里犯了错误吗?

谢谢!

4

1 回答 1

1

销毁函数的资源 url 实际上是 'user_votes/:id'。:id所以调用link_to助手时需要传入参数

 = link_to 'Delete', {controller: "user_votes", action: "destroy", recipient_uid: friend.uid, id: some_id}, method: "delete"

替换some_id为实际的资源 ID。

于 2013-11-11T17:32:20.160 回答