Inviting Users with devise_invitable Discussion | GoRails
Having trouble getting started with the devise invitable gem - rubyonrails-talk - Ruby on Rails Discussions
Newest 'devise-invitable' Questions - Stack Overflow
ruby on rails - Devise Invitable display invited users - Stack Overflow
Videos
I cannot find any good tutorials on how to use devise_invitable and multiple companies.
I have used this gem successfully for 6 years, where a User belongs_to 1 company.
Now we need to allow users to be able to switch between companies, and get invited to multiple ones.
The switching between companies is now possible, using a new model:
class UserCompany < ApplicationRecord belongs_to :user belongs_to :company end
Users can now switch between companies, and we use the User.company_id to remember which company is the 'current selected' one.
How do we upgrade the Invitation part to support multiple companies?
-
A user could get 2 invites at the same time from 2 different companies.
We have created the app/controllers/invitations_controller.rb and added the route for that.
I am just stuck at what happens next, I guess I need some magic in the create method and should we use super there?
The invitation_* variables have been moved from the User to UserCompany model, along with is_admin which is used to mark which users have admin rights in that company.
#<UserCompany:0x000055951d8358d0 id: 5, user_id: 1, company_id: 1, is_admin: true, disabled: false, created_at: Mon, 25 Sep 2023 15:53:31.461755000 UTC +00:00, updated_at: Mon, 25 Sep 2023 16:05:35.771460000 UTC +00:00, invitation_token: nil, invitation_created_at: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_type: nil, invited_by_id: nil, invitations_count: 0>
This is the original controller for devise_invitable: https://github.com/scambra/devise_invitable/blob/master/app/controllers/devise/invitations_controller.rb
This is where we are at now, could be totally wrong
class InvitationsController < Devise::InvitationsController
def create
invited_user = User.invite!(
email: params[:user][:email],
company_id: current_user.company_id
)
UserCompany.create(
user_id: invited_user.id,
company_id: current_user.company_id
)
super
endI have a similar setup, have you tried something like this in the create section of your invitations controller?
This approach works for me, but you'll need to override the default invitations controller do it this way.
def create
@invited_user = User.invite!({:email => "[email protected]"}, current_user)
@invited_user.update(organization: current_organization)
...
end
I have current_organization defined in my application_controller.rb file as:
def current_organization
@current_organization ||= current_user.organization
end
if you use devise invitable, Overwrite your invitations controller's new and update actions. In the new action associate the new user to the group, in the update action permit the foreign key and you should be good to go.