开发者

How do you access an instance variable in an Authlogic config block?

开发者 https://www.devze.com 2022-12-13 16:55 出处:网络
Given a user mode开发者_开发知识库l something along the lines of: class User < ActiveRecord::Base

Given a user mode开发者_开发知识库l something along the lines of:

class User < ActiveRecord::Base

  acts_as_authentic do |config|
    config.validate_email_field = true
  end

end

I would like to be able to modify "true" to be specific to the user that is signing up. I would like to be able to do something like:

class User < ActiveRecord::Base

  acts_as_authentic do |config|
    config.validate_email_field = ['dont@validate.me'].include?(instance_email)
  end

end

but instance_email isn't available there. Do you have to override a method on the user model in order to access this variable? How can this be done?


You can't because the Authlogic block is evaluated when the class is loaded and it has no instance context.

You need to write your own validator.

class User < ...

  validate :validate_email

  ...

  def validate_email
    if !['dont@validate.me'].include?(instance_email)
      # write here your validation logic
    end
  end 

end
0

精彩评论

暂无评论...
验证码 换一张
取 消