Goal
I would like to let a user.admin invite a user to only one of its hotels with the devise_invite
gem.
=> User
and Hotel
are connected via a Join table UserHotel
.
Issue
I started to doubt the way I set up devise(_invitable). With the way I currently set it up, I am not able to create the user_hotel Join_table and/or add the specific hotel params to the invited user, see output below for checks:
controller
>> @user.hotels => #<ActiveRecord::Associations::CollectionProxy []>
console:
pry(main)> User.invitation_not_accepted.last.hotels
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."invitation_token" IS NOT NULL AND "users"."invitation_accepted_at" IS NULL ORDER BY "users"."id" DESC LIMIT $1 [["LIMIT", 1]]
Hotel Load (0.4ms) SELECT "hotels".* FROM "hotels" INNER JOIN "user_hotels" ON "hotels"."id" = "user_hotels"."hotel_id" WHERE "user_hotels"."user_id" = $1 [["user_id", 49]]
=> []
UPDATE
The issue seems to be in the many-to-many relationship between user and hotel. When I break my controller 'new' action after hotel.user.new
and test it I het the following:
>> @user.hotels => #<ActiveRecord::Associations::CollectionProxy []>
>> @hotel.users => #<ActiveRecord::Associations::CollectionProxy [#<User id: 2, email: "test@hotmail.com", created_at: "2019-11-05 14:17:46", updated_at: "2019-11-05 15:04:22", role: "admin">, #<User id: nil, email: "", created_at: nil, updated_at: nil, role: "admin">]>
Note I set up users with devise, such that my users controller is build up as:
Code
routes
Rails.application.routes.draw do
devise_for :users
resources :hotels do
devise_for :users, :controllers => { :invitations => 'users/invitations' }
end
end
models
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
has_many :user_hotels, dependent: :destroy
has_many :hotels, through: :user_hotels
accepts_nested_attributes_for :user_hotels
enum role: [:owner, :admin, :employee]
after_initialize :set_default_role, :if => :new_record?
def set_default_role
self.role ||= :admin
end
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :invitable
end
class UserHotel < ApplicationRecord
belongs_to :hotel
belongs_to :user
end
class Hotel < ApplicationRecord
has_many :user_hotels, dependent: :destroy
has_many :users, through: :user_hotels
accepts_nested_attributes_for :users, allow_destroy: true, reject_if: ->(attrs) { attrs['email'].blank? || attrs['role'].blank?}
end
views/hotels/show
<%= link_to "invite new user", new_user_hotel_invitation_path(@hotel)%>
controllers/users/invitations_controller.rb
class Users::InvitationsController < Devise::InvitationsController
def new
@hotel = Hotel.find(params[:hotel_id])
@user = @hotel.users.new
end
def create
@hotel = Hotel.find(params[:hotel_id])
@user = @hotel.users.new(hotel_user_params)
@user.invite!
end
private
def hotel_user_params
params.require(:user).permit(:email, :role,
hotel_users_attributes: [:hotel_id])
end
end
views/invitations/new.html.erb
<h2><%= t "devise.invitations.new.header" %></h2>
<%= simple_form_for(resource, as: resource_name, url: user_hotel_invitation_path(@hotel), html: { method: :post }) do |f| %> <%= f.error_notification %>
<% resource.class.invite_key_fields.each do |field| -%>
<div class="form-inputs">
<%= f.input field %>
</div>
<% end -%>
<%= f.input :role, collection: [:owner, :admin, :employee] %>
<div class="form-actions">
<%= f.button :submit, t("devise.invitations.new.submit_button") %>
</div>
<% end %>
I have something working, even tough I am not sure if this is the best way to do it (read: I highly doubt it).
class Users::InvitationsController < Devise::InvitationsController
def new
@hotel = Hotel.find(params[:hotel_id])
@user = @hotel.users.new
@user.hotels << @hotel
end
def create
@hotel = Hotel.find(params[:hotel_id])
@user = @hotel.users.new(hotel_user_params)
@user.hotels << @hotel
@user.invite!
end