Passing the validates_format_of method

I want to do this:

validates_format_of :email, :with => email_regex

def email_regex
  email_name_regex  = '[A-Z0-9_\.%\+\-]+'
  domain_head_regex = '(?:[A-Z0-9\-]+\.)+'
  domain_tld_regex  = '(?:[A-Z]{2,4}|museum|travel)'
  return /\A#{email_name_regex}@#{domain_head_regex}#{domain_tld_regex}\z/i
end

      

but I am getting a message that I need to pass a regex to the validates method. I was confident that you can do something like this. Am I missing a step?

thanks

0


a source to share


2 answers


Your code looks fine. And I think you are defining a method email_regex

before it is used by the validation method.

You need to debug what gets validates_format_of

(change RoR code to print: with argument type). This should be pretty simple and help you fix the problem.



Now I can only assume that the method is being email_regex

overridden somewhere and returns something other than Regexp.

0


a source


How about using contant? This would also ensure that the regex is only compiled once.

class ...
  FOO_BAR = /#{baz}.../

  validatess ... :with => FOO_BAR

      



but yes i am not sure why your way is not working.

0


a source







All Articles