gpt4 book ai didi

ruby-on-rails - 不同的模型如何在 Rails 中使用相同的登录页面

转载 作者:太空宇宙 更新时间:2023-11-03 16:16:04 25 4
gpt4 key购买 nike

这是我的第一个 Rails 应用程序,我对 Rails 还很陌生。我用设计创建了一个用户模型,使用 pundit 向用户模型添加了角色(管理员,所有者)。

用户.rb

    class User < ApplicationRecord
has_many :owners, dependent: :destroy

enum role: [:user, :owner, :agent, :admin]
after_initialize :set_default_role, :if => :new_record?

def set_default_role
self.role ||= :user
end

# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end

用户政策:

    class UserPolicy
attr_reader :current_user, :model

def initialize(current_user, model)
@current_user = current_user
@user = model
end

def index?
@current_user.admin?
end

def show?
@current_user.admin? || @current_user == @user
end

def update?
@current_user.admin?
end

def destroy?
return false if @current_user == user
@current_user.admin?
end
end

我创建了一个所有者模型。新所有者只能由管理员角色创建。管理员仅将所有者添加到所有者模型。管理员添加所有者后,所有者电子邮件和密码将发送到所有者电子邮件。使用给定的凭据所有者登录到他的页面。在这里,我想要一个登录页面,供所有者登录到他的页面。我尝试使用所有者的用户登录页面进行登录,但出现错误用户名和密码无效。是否可以为所有者使用用户登录,这样我只有一个登录名供管理员/所有者登录到他们的页面,或者我应该为所有者创建另一个登录页面?

所有者.rb:

    class Owner < ApplicationRecord
has_many :customers, dependent: :destroy
has_many :agents, dependent: :destroy
belongs_to :user

enum role: [:user, :owner]
after_initialize :set_default_role, :if => :new_record?

def set_default_role
self.role ||= :owner
end

before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :mobile, presence: true, length: { maximum: 10 }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true

def Owner.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end

end

我的 owner_policy:

    class OwnerPolicy
attr_reader :current_user, :owner

def initialize(current_user, owner)
@current_user = current_user
@owner = owner
end

def index?
@current_user.admin?
end

def new?
@current_user.admin?
end

def create?
@current_user.admin?
end

def show?
@current_user.admin? || @current_user == @owner
end

def edit?
@current_user.admin? || @current_user == @owner
end

def update?
@current_user.admin? || @current_user == @owner
end

def destroy?
return false if @current_user == owner
@current_user.admin?
end
end

我用谷歌搜索并尝试了很多答案来实现,但最终都以一团糟告终。通过尝试不同的答案把我带到了兔子洞。任何人,请帮助我。提前致谢

最佳答案

以下是我认为您应该如何实现您尝试要实现的目标:

class User < ApplicationRecord
# This is an **abstract** base class.
# You should not create a `User` directly;
# Only create Admin, Owner and Agent records

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

before_save { self.email = email.downcase }

validates :name, presence: true,
length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true,
length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :mobile, presence: true,
length: { maximum: 10 }
has_secure_password
validates :password, presence: true,
length: { minimum: 6 },
allow_nil: true

def self.digest(string)
cost = if ActiveModel::SecurePassword.min_cost
BCrypt::Engine::MIN_COST
else
BCrypt::Engine.cost
end
BCrypt::Password.create(string, cost: cost)
end
end

class Admin < User
# admin.type == 'Admin'
# -- use the default column name for Single Table Inheritance.

has_many :owners, dependent: :destroy
end

class Owner < User
# owner.type == 'Owner'

belongs_to :admin
has_many :customers, dependent: :destroy
has_many :agents, dependent: :destroy
end

class Agent < User
# agent.type == 'Agent'

# Add any agent-specific logic in here.
# (This wasn't discussed in your original question)
end

您现在可以在其他地方继续继承模式 - 例如在您的策略中:

class OwnerPolicy < UserPolicy

登录时,您始终可以引用 User 模型 - 因为所有用户类型都继承自此。

关于ruby-on-rails - 不同的模型如何在 Rails 中使用相同的登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44754315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com