开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜