我的控制器中的操作有问题 - 当我在视图中使用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"
你能告诉我我犯了哪个错误吗?
谢谢!
答案 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。