Rails attr_accessible :as 和自定义验证器
我有模型用户
class User < ActiveRecord::Base
has_and_belongs_to_many :roles
attr_accessible :login, :email, :password, :password_confirmation ...
attr_accessible :role_ids, :active, :as => :super_admin
validates :email, :presence => true,
:format => {:with => /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i},
:uniqueness => {:case_sensitive => false},
:restricted_email_domain => true
...
end
和两个单独的前端和后端用户控制器。第一个非常简单并且运行良好,第二个在下面
class Admin::UsersController < Admin::BaseController
def create
@user = User.new
@user.assign_attributes(params[:user], :as => :super_admin)
if @user.save
...
end
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user], :as => :super_admin)
...
end
end
我正在使用自定义验证器来检查用户的电子邮件域是否受到限制
class RestrictedEmailDomainValidator < ActiveModel::EachValidator
def validate_each(record, attr_name, value)
if !value.include?("@") # this value is nil if I add ":as => :super_admin"
record.errors.add(attr_name, :invalid, options.merge(:value => value))
else
domain = value.split("@")[1]
record.errors.add(attr_name, :restricted_email_domain, options.merge(:value => value)) if ::RestrictedEmailDomain.where(:domain => domain).exists?
end
end
end
module ActiveModel::Validations::HelperMethods
def validates_restricted_email_domain(*attr_names)
validates_with RestrictedEmailDomainValidator, _merge_attributes(attr_names)
end
end
更新操作运行良好但创建操作给出 当您没有想到时您有一个 nil 对象它!
config/initializers/restricted_email_domain_validator.rb:3:'validate_each'
中出现错误。没有 :as =>; :super_admin
一切正常,但当然 role_ids
和 active
属性没有分配。
我可以手动赋值,但我认为这不是一个完美的解决方案。
I have model User
class User < ActiveRecord::Base
has_and_belongs_to_many :roles
attr_accessible :login, :email, :password, :password_confirmation ...
attr_accessible :role_ids, :active, :as => :super_admin
validates :email, :presence => true,
:format => {:with => /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i},
:uniqueness => {:case_sensitive => false},
:restricted_email_domain => true
...
end
and two separate user controllers for front-end and back-end. First one is very trivial and works well, second one is below
class Admin::UsersController < Admin::BaseController
def create
@user = User.new
@user.assign_attributes(params[:user], :as => :super_admin)
if @user.save
...
end
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user], :as => :super_admin)
...
end
end
I'm using custom validator that checks whether user's email domain restricted or not
class RestrictedEmailDomainValidator < ActiveModel::EachValidator
def validate_each(record, attr_name, value)
if !value.include?("@") # this value is nil if I add ":as => :super_admin"
record.errors.add(attr_name, :invalid, options.merge(:value => value))
else
domain = value.split("@")[1]
record.errors.add(attr_name, :restricted_email_domain, options.merge(:value => value)) if ::RestrictedEmailDomain.where(:domain => domain).exists?
end
end
end
module ActiveModel::Validations::HelperMethods
def validates_restricted_email_domain(*attr_names)
validates_with RestrictedEmailDomainValidator, _merge_attributes(attr_names)
end
end
Update action works well but create action gives You have a nil object when you didn't expect it!
error in config/initializers/restricted_email_domain_validator.rb:3:in 'validate_each'
. Without :as => :super_admin
everything is ok, but of course role_ids
and active
attributes are not assigned.
I can assign values manually, but I think it is not a perfect solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能是错的,因为我现在没有办法检查它,但我认为当你编写时,
@user.assign_attributes(params[:user], :as => :super_admin) - 仅设置这两个字段,其余字段保持默认状态。
你尝试过类似的事情吗
I may be wrong, as I don't have a way to check it now, but I think that when you write
@user.assign_attributes(params[:user], :as => :super_admin) - only sets these two fields, leaving the rest in default state.
Did you try something like