ActionController:没有路由匹配

时间:2013-11-11 17:11:55

标签: ruby-on-rails ruby-on-rails-4

我的控制器中的操作有问题 - 当我在视图中使用destroy操作调用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

user_vote_controller:

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"

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

谢谢!

1 个答案:

答案 0 :(得分:1)

destroy函数的资源url实际上是'user_votes /:id'。所以当你调用link_to helper

时,你需要传递:id参数
 = link_to 'Delete', {controller: "user_votes", action: "destroy", recipient_uid: friend.uid, id: some_id}, method: "delete"

some_id替换为实际的资源ID。