具有管理员角色的用户可以拥有多家酒店,但管理员如何才能邀请用户只访问其中一家酒店



上下文我无法理解以下内容:

  • User和Hotel之间通过联接表User_Hotel存在多对多关系
  • 使用设备创建的用户具有管理员角色
  • 具有管理员角色的用户可以创建许多酒店
  • 具有管理员角色的用户应能够邀请其他用户到特定酒店(例如,并非所有酒店(。我正在使用设计可邀请的宝石发送邀请

问题我为用户/邀请设置了路线、模型和控制器,但出现了问题:

  1. 因为我的hotel_id参数没有正确发送到我的invitations_controller。请参阅错误消息:Couldn't find Hotel without an ID. params sent: {"format"=>"109"}
  2. 我不确定我是否/如何在被邀请的特定酒店用户之间建立联系

视图/酒店/展会

<%= link_to "invite new user", new_user_invitation_path(@hotel) %>

路线

Rails.application.routes.draw do
devise_for :users, controllers: {
invitations: 'users/invitations'
}
resources :hotels do
resources :users
end
end

型号

class User < ApplicationRecord
has_many :user_hotels, dependent: :destroy
has_many :hotels, through: :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

控制器/用户/邀请

class Users::InvitationsController < Devise::InvitationsController
def new
@hotel = Hotel.find(params[:hotel_id])
@user = User.new
How to build the join table UserHotel when inviting?
end
end

IDK,如果被邀请到酒店的用户是否需要接受。

如果需要接受:

您需要一个表来存储Invitations,其中将存储user ainvitation到user bhotel a。那么您需要inviter_idinvited_idhotel_id。当用户接受邀请时,您将在与酒店和用户相关的中间表中添加一条记录。

另一种方法是在中间表中添加一个标志来控制哪些是已接受的,哪些是尚未接受的,并在关系上添加一个默认范围以仅获得已接受的。

如果用户将直接添加到酒店:

你只需要做:

@hotel = Hotel.find(params[:hotel_id])
@user = User.new
@user.hotels << @hotel

相关内容

最新更新