Rails: 邀请注册为设计好友 gem

Rails: invite to sign up as friend with devise gem

我在 Rails 应用程序中使用设计。我希望用户可以给他们的朋友一个 link,通过 link,他们可以注册并与他们的邀请人成为朋友。

这是我想出的。

首先,我给每个用户一个唯一的代码,比如dbgadf34t42a,人们可以像localhost/signup?invite_code=dbgadf34t42a一样访问url。然后自定义Devise的sign up controller,使用这段代码找到邀请人,然后创建friendship关系

代码类似于

  def create
    #Create user account, and then create Friendship
    invite_code = paramsp[:invite_code]
    if user.save
      inviter = User.find_by_invite_code(invite_code)
      inviter.friendships.create(friend: user)
    end
  end

我在想

  1. 如果有更好的方法,宝石或代码。
  2. 我没有在customize Devise的Signup Controller上找到好的资源,我可以把逻辑提取到另一个地方,存储邀请码,然后继续创建friendship模型吗?

可能是这样的:

  def after_sign_up
    if params[:invite_code]
      redirect_to new_friendship_path
    else
      # default_behavior
    end
  end
  1. If there is any better way of doing this, gems or codes.

您可以使用 devise-inviteable gem。对于创建友谊,您可以自定义此 gem.

  1. I didn't find good resources on customize Devise's Signup Controller, can I extract the logic to another place, store the invite code, and then continue creating friendship model?

要自定义 Devise Signup,您需要继承 Devise::RegistrationsController

class RegistrationsController < Devise::RegistrationsController
   def create
     super
     # Now write your customize code for friendship
   end
end

然后告诉设计使用该控制器而不是默认控制器:

# app/config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}