【发布时间】:2020-02-25 12:16:42
【问题描述】:
上下文 我无法理解以下内容:
- User 和 Hotel 之间通过连接表 User_Hotel 存在多对多关系。
- 使用 devise 创建的用户具有管理员角色
- 具有管理员角色的用户可以创建许多酒店
- 具有管理员角色的用户应该能够邀请其他用户到特定酒店(例如,不是所有酒店)。我正在使用 devise-invitable gem 发送邀请。
问题 我为用户/邀请设置了路由、模型和控制器,但出了点问题:
- 因为我的hotel_id 参数未正确发送到我的invitations_controller。查看错误信息:
Couldn't find Hotel without an ID. params sent: {"format"=>"109"} - 我不确定是否/如何在受邀的特定酒店用户之间建立联系?
观看次数/酒店/演出
<%= 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
【问题讨论】:
标签: ruby-on-rails devise devise-invitable