目标
我想让user.admin使用devise_invite gem邀请用户仅去其旅馆之一。
=>用户和酒店通过Join_table UserHotel连接起来。
问题
我无法创建user_hotel Join_table和/或将特定的酒店参数添加到受邀用户。
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]]
=> []
更新
问题似乎出在用户与酒店之间的多对多关系中。当我在hotel.user.new
之后中断控制器的“新”操作并对其进行测试时,我会提示以下内容:
-
>> @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">]>
注意 我使用devise设置了用户,以便将我的用户控制器构建为:
- users / confirmations_controller.rb
- users / invitations_controller.rb
- users / omniauth_callbacks_controller.rb
- users / password_controller.rb
- users / registrations_controller.rb
- users / sessions_controller.rb
- users / unlocks_controller.rb
代码
路线
Rails.application.routes.draw do
devise_for :users
resources :hotels do
devise_for :users,:controllers => { :invitations => 'users/invitations' }
end
end
模型
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
观看次数/酒店/显示
<%= 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,:employee] %>
<div class="form-actions">
<%= f.button :submit,t("devise.invitations.new.submit_button") %>
</div>
<% end %>