Ask A Question

Notifications

You’re not receiving notifications from this thread.

devise_invitable: How to create a guest only after user confirmation by email

rodolfo asked in Gems / Libraries

I'm trying to create members of a team by using devise_invitable. But by looking the video related to devise_invitable, I'm not sure if I can create team members ONLY AFTER user confirmation by email. Any ideas?
Thank you

Reply

Hey rodolfo,

What problem are you trying to solve by creating the member only after the confirmation?

Reply

Hi @Jacob, thank you for your answer. I have 3 models: user, team and member. I'm trying to create a member by using devise_invited, but watching the devise_invited tutorial my app is currently creating a member of a team even if the invited user has not accepted the invitation by email. I wont to create a member only after the confirmation. Otherwise I will have members that do not exist as user. Here a fragment of the code:

Rails.application.routes.draw do
  resources :teams do
    resources :members
  end
  devise_for :users
  root to: 'teams#index'
end
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :invitable, :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatables
  has_many :own_teams, class_name: 'Team', foreign_key: :user_id
  has_many :members
  has_many :teams, through: :members
end
class Team < ApplicationRecord
  belongs_to :user
  has_many :members
  has_many :users, through: :members
end
class Member < ApplicationRecord
  belongs_to :user
  belongs_to :team
  attribute :email, :string
  before_validation :set_user_id, if: :email?
  def set_user_id
    self.user =  User.invite!(email: email)
  end
end
class MembersController < ApplicationController
  before_action :authenticate_user!
  before_action :set_team
    def create
      member = @team.members.new(member_params)
      member.team = @team
      if member.save
        redirect_to @team, success: 'Bien hecho'
      else
        redirect_to @team, alert: 'Mal hecho'
      end
    end
    private
      def set_team
        @team = current_user.own_teams.find(params[:team_id])
      end
      def member_params
        params.require(:member).permit(:email)
      end
end
Reply

Ok I see, you could put a status field on the member object so you can filter out members who haven't accepted their invitation and then change the status once the user has accepted the invite. This would allow you to periodically delete invitations that are over X days old so you don't end up with a bunch of abandoned records.

Another option would be to store the team ID on the invited user object so once the user accepts their invitation, you then create the member object since that really only functions as a join table between the user object and the team object.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,400+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.