我有一个Rails应用,其中Users
可以邀请Members
到Teams
,为此,我尝试使用gem devise_invitable
。它到目前为止工作正常,但在删除待处理的邀请时遇到了一些问题。例如,如果用户输入了错误的电子邮件地址,我希望他/她能够删除邀请。但我无法让它发挥作用。我的路线设置如下:
devise_for :users, :controllers => {sessions: "users/sessions", registrations: "users/registrations", invitations: "users/invitations"}
resources :teams do
resources :invitations
[...]
我的自定义"users/invitations"
(置于controllers / users / invitations_controller.rb)控制器的相关部分如下所示:
class Users::InvitationsController < Devise::InvitationsController
before_action :set_invitation, only: [:destroy]
def destroy
This currently never gets called
team = @invitation.team
@invitation.destroy
respond_to do |format|
format.html { redirect_to edit_team_path(team) }
format.json { head :no_content }
end
end
我尝试像这样链接destroy
动作:
= link_to team_invitation_path(@team, invitation), method: :delete, data: { confirm: 'Are you sure you want to delete this invitation?'}, class: "label label-danger label-pill pull-xs-right" do
= icon("trash-o")
我的Invitation
模型如下所示:
class Invitation < ActiveRecord::Base
belongs_to :member
belongs_to :team
validates_presence_of :recipient_email
enum status: [ :pending, :accepted ]
end
单击链接时,我当前收到以下错误:
Routing Error
uninitialized constant InvitationsController
我不完全确定如何解决它或我可以错过的东西。不是100%devise_invitable
是我现在需要的正确宝石。
关于如何使其发挥作用的任何想法?
运行rake routes
后的相关路线:
# These were here before as well
accept_user_invitation GET /users/invitation/accept(.:format)
users/invitations#edit
remove_user_invitation GET /users/invitation/remove(.:format) users/invitations#destroy
user_invitation POST /users/invitation(.:format) users/invitations#create
new_user_invitation GET /users/invitation/new(.:format) users/invitations#new
# My custom / new team invitation routes
team_invitations
GET /teams/:team_id/invitations(.:format) users/invitations#index
POST /teams/:team_id/invitations(.:format) users/invitations#create new_team_invitation
GET /teams/:team_id/invitations/new(.:format) users/invitations#new edit_team_invitation
GET /teams/:team_id/invitations/:id/edit(.:format) users/invitations#edit team_invitation
GET /teams/:team_id/invitations/:id(.:format) users/invitations#show
PATCH /teams/:team_id/invitations/:id(.:format) users/invitations#update
PUT /teams/:team_id/invitations/:id(.:format) users/invitations#update
DELETE /teams/:team_id/invitations/:id(.:format) users/invitations#destroy
新路线指向例如users/invitations#destroy
答案 0 :(得分:2)
我假设你试图指向Users::InvitationsController
,这与InvitationsController
不同 - 它不存在。您需要通知routes.rb
:
resources :teams do
resources :invitations, module: 'users'
...
end
end