Ruby on rails 不同型号如何在rails中使用相同的登录页面
这是我的第一个rails应用程序,我对rails还很陌生。我使用Desive创建了一个用户模型,并使用pundit向用户模型添加了角色(管理员、所有者) user.rbRuby on rails 不同型号如何在rails中使用相同的登录页面,ruby-on-rails,ruby,ruby-on-rails-4,devise,pundit,Ruby On Rails,Ruby,Ruby On Rails 4,Devise,Pundit,这是我的第一个rails应用程序,我对rails还很陌生。我使用Desive创建了一个用户模型,并使用pundit向用户模型添加了角色(管理员、所有者) user.rb class User < ApplicationRecord has_many :owners, dependent: :destroy enum role: [:user, :owner, :agent, :admin] after_initialize :set_default_role, :if
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
我创建了一个所有者模型。新所有者仅由管理员角色创建。管理员只将所有者添加到所有者模型中。管理员添加所有者后,所有者电子邮件和密码将发送到所有者电子邮件。使用给定的凭据,所有者登录到其页面。这里我想要一个登录页面,让所有者登录到他的页面。我试图使用用户的登录页面由所有者登录,但我得到错误无效的用户名和密码。是否可以为所有者使用用户登录,这样我只有一个管理员/所有者登录到他们的页面,或者我应该为所有者创建另一个登录页面
owner.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
我在谷歌上搜索并尝试了很多答案,但结果都是一团糟。它通过尝试不同的答案把我带到兔子洞。任何人,请帮助我。提前感谢以下是我认为您应该如何实现您想要实现的目标:
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用户
现在,您可以在其他地方继续继承模式,例如在您的策略中:
class OwnerPolicy < UserPolicy
类所有者策略
登录时,您始终可以引用
用户
模型-因为所有用户类型都继承自此。您的用户
模型是否具有用户名
和密码
,这可能是管理员
或所有者
的登录用户?我对您的数据库模型感到困惑。用户可以是四种类型之一:[:用户、所有者、代理、管理员]
。一个所有者用户
拥有许多所有者
,现在您希望能够作为所有者之一登录
??!!这可能是您真正想要的:classowner
,使用Owner.role==:Owner
。现在您只有一个用户模型(User
)。同意@Tom lord感谢您的支持response@TomLord. 我同意你的看法,但我在用户和所有者之间只有很少的类似列(如姓名、电子邮件、密码)。如果我继承了owner所有者
,另一个表单用于创建/编辑代理
,另一个表单用于创建和编辑管理员
。您可以拥有一个OwnersController
,使用OwnersController#new/create/edit/update
操作。在rails中查找单表继承,并遵循标准设计模式。
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