加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在Rails中发送项目邀请

发布时间:2020-12-17 03:34:17 所属栏目:百科 来源:网络整理
导读:嘿所有,我正在寻找一种方法来添加我的Rails应用程序的邀请策略.我正在使用Devise进行身份验证,就像 devise_invitable的外观一样,但据我所知,该gem只允许您邀请新用户加入系统. 在我的应用程序中,用户可以邀请其他用户(使用电子邮件)加入他当前的项目.如果该
嘿所有,我正在寻找一种方法来添加我的Rails应用程序的邀请策略.我正在使用Devise进行身份验证,就像 devise_invitable的外观一样,但据我所知,该gem只允许您邀请新用户加入系统.

在我的应用程序中,用户可以邀请其他用户(使用电子邮件)加入他当前的项目.如果该电子邮件地址存在,则添加用户;如果地址不存在,我想向该电子邮件地址发送特定于项目的邀请.如果用户已拥有帐户,则可以登录并将其帐户绑定到该项目.如果没有,她可以创建一个新帐户.

有没有人建议在哪里寻找这样的系统?

解决方法

# app/models/invite.rb
class Invitation < ActiveRecord::Base
  validates_uniqueness_of :email,:scope => :project_id
  belongs_to :project
  has_many :users
  after_save :email_invite_if_no_user

  private
    def email_invite_if_no_user
      unless User.find_by_email(email)
        UserMailer.send_invite(self).deliver
      end
    end
end

# config/routes.rb
resources :projects do
  resources :invites
end

# app/controllers/invites_controller.rb
class InvitesController < ActionController
  before_filter :get_project

  def new
    # render invite form
  end

  def create
    @invite = Invite.new(params[:invite])
    @invite.project_id = @project.id
    if @invite.save
      flash[:message] = "Successfully invited #{params[:invite][:email]}"
      redirect_to @project
    else
      flash[:error] = "Could not invite #{params[:invite][:email]}"
      render :new
    end
  end

  private
    def get_project
      @project = Project.find(params[:project_id])
    end 
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读