邀请和删除协作者 to/from 项目 - Rails

Inviting and removing collaborators to/from project - Rails

我正在构建一个 rails 应用程序,其中包含可以邀请用户(由 Devise 处理)作为协作者的项目(很像 GitHub 存储库和协作者)。我正在努力获得邀请 运行(特别是删除合作者)。我一直在关注 this tutorial 以创建邀请 运行,但教程不包括撤销邀请(删除合作者)。

我有一个 projects_controller.rb 文件和一个 invites_controller.rb 文件。邀请控制器处理新邀请的创建,工作正常(即:如果用户已经存在,他们会立即添加到项目中,如果用户不存在,邀请将发送到输入的电子邮件地址)。

我应该如何添加删除协作者的功能?对我来说,使用 invites#destroy 是合乎逻辑的(因为邀请是在该控制器中创建的)但是,简单地删除邀请不会撤销用户对项目的许可。而最初创建项目的用户,他们根本不会收到邀请..

有人知道我应该走哪条路吗? 让我知道是否有更多信息有帮助。

谢谢

使用 gem 'devise_invitable' 处理邀请。 https://github.com/scambra/devise_invitable

假设您使用 has_many 关联处理项目的协作者:

class Project < ApplicationRecord
  has_many :users
end

然后您可以实施 invites#destroy 从协作者列表中删除用户:

class InvitesController < ApplicationController
  def destroy
    @project = Project.find params[:project_id]
    @user = User.find params[:user_to_remove]
    @project.users.delete(@user)
    # Add whatever renders or redirects you need to here
  end
end

您的视图可以使用此按钮删除协作者:

# Make sure @project (the project to remove from) and @user (the user to remove) are defined and non-nil
<%= link_to "Remove Collaborator", url_for(:controller => :invites, :action => :destroy, :project_id => @project.id, :user_to_remove => @user.id), :method => :delete %>