如何解决带有一个使用装置的控制器的两种模型,验证问题和错误消息
我的应用程序中有两个模型,一个是带有设计的“用户”模型,一个是由脚手架制作的“员工”模型。 我需要一种方法来在新用户注册后立即填充employee表,这两个表共享一些参数,有些是排他的。员工属于用户模型,每个用户只有一个员工。 我使用的视图是具有嵌套属性的devise用户注册表单,以允许雇员使用params。创建由用户控制器处理。 发生的问题:
- 在创建第一个用户之前,在空密码字段中使用未定义的方法'email'
- 无法使雇员模型的专有“ name”参数的错误消息显示在与用户模型的错误消息相同的位置
员工模型:
class Employee < ApplicationRecord
audited
validates :email,presence: true,uniqueness: true
validates :name,presence: true
belongs_to :user,inverse_of: :employee,dependent: :destroy,optional: true
用户模型
class User < ApplicationRecord
audited
# Include default devise modules. Others available are:
# :confirmable,:lockable,:timeoutable,:trackable and :omniauthable
devise :database_authenticatable,:registerable,:recoverable,:rememberable
has_one :employee,inverse_of: :user
accepts_nested_attributes_for :employee
validates_presence_of :password
validates :password,format: { with: /(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[["§@?!`´~#'._,;<>|°()"{}="@$%^&*+-]]).{8,}/},if: -> {password.present?}
validates_presence_of :password_confirmation,if: -> {password.present? and password =~ /(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[["§@?!`´~#'._,}/}
validates_confirmation_of :password,if: -> {password.present? and password_confirmation.present? and password =~ /(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[["§@?!`´~#'._,}/}
validates_presence_of :email
validates :email,format: { with: /\A([^@[0-9]\s]+\.)+([^@[0-9]\s]+)@((thisapp+\.)+com)\z/i},uniqueness: true,if: -> {email.present?}
end
用户控制器
class Users::RegistrationsController < Devise::RegistrationsController
# before_action :configure_sign_up_params,only: [:create]
# before_action :configure_account_update_params,only: [:update]
# GET /resource/sign_up
def new
super
end
def new_employee
@employee = Employee.new
end
# POST /resource
def create
@employee = Employee.new(employee_params)
super
@employee.email = User.last.email
@employee.user_id = User.last.id
@employee.created_by_id = User.last.id
@employee.save
end
用户注册视图
.card style='text-align:center;'
.card-body
h2.card-title style='text-align:center;' = t('.sign_up')
= form_for(resource,as: resource_name,url: registration_path(resource_name)) do |f|
= render "users/shared/error_messages",resource: resource
br
.field
= f.label :email,t('users.email_field')
br
= f.text_field :email,autofocus: true,autocomplete: "email"
.field
= f.label :password,t('users.password_field')
/- if @minimum_password_length
em
= t('devise.shared.minimum_password_length',count: @minimum_password_length)
br
= f.password_field :password,autocomplete: "new-password"
.field
= f.label :password_confirmation,t('users.password_confirmation_field')
br
= f.password_field :password_confirmation,autocomplete: "new-password"
= fields_for :employee do |e|
= e.label :name
= e.text_field :name
因此,当我在控制器中使用@employee.email = User.last.email
时,如果不填写密码字段,则会收到“未知方法'email”错误,除非我已有用户,否则将收到自定义错误消息填写电子邮件。我猜是因为我正在寻找目前尚不存在的最后一个用户。可以播种用户,但这似乎有点黑。使用@employee.email = User.last(params[:email])
进行了尝试,这导致电子邮件被另存为某些哈希值,但是至少我收到了错误消息。有没有办法将该哈希再次转换为真实的电子邮件地址?
另一个问题是名称字段的验证。要求在员工模型中进行验证,并且用户模型接受嵌套的属性,但这似乎还不够。
我做到了
if params[:employee][:name].blank?
flash[:notice] = t('.noname')
在无法在名称字段中没有任何值的情况下提交表单的情况下起作用,但是却弄乱了我的错误消息。显示一条Flash消息,其中所有其他错误(无电子邮件/密码/密码确认)由设备的共享错误消息作为非Flash消息处理:
- resource.errors.full_messages.each do |message|
li
= message
因此,将空白名称用作Flash消息看起来会不一致,并且Flash消息的位置已经保留。
Flash消息位于顶部,“以下错误防止..”,实际错误消息位于“ Registrieren”下方,这也是空白名称错误消息所在的地方。
有什么想法可以解决这个问题,或者比在用户控制器中处理这些问题更好的解决方案?
解决方法
class Users::RegistrationsController < Devise::RegistrationsController
# GET /resource/sign_up
def new
super
end
def new_employee
@employee = Employee.new
end
def sign_up_params
params.require(:user).permit(:email,:password,:password_confirmation,employee_attributes: %i[name])
end
从devise注册控制器创建用户时将使用此注册参数。由于我们使用了嵌套属性,因此将参数与父对象一起传递将处理员工创建(https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html)
用于为员工分配电子邮件
-
一种方法是合并来自用户参数的电子邮件。
def sign_up_params sign_up_param = params.require(:user).permit(:email,employee_attributes: %i[name]) sign_up_param[:employee_attributes].merge!(email: params[:user][:email]) sign_up_param end
-
或者您可以在验证之前从用户分配员工的电子邮件。
类雇员
validates :email,presence: true,uniqueness: true validates :name,presence: true belongs_to :user,inverse_of: :employee,dependent: :destroy,optional: true # Callbacks before_validation :set_email # Methods # Set email of the employee from the user. def set_email self.email = self.user.email end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。