I have a variable called "account_type" passed from a rails form and need to access the value in the corresponding model.
I can check if :account_type exists as a symbol, but where does the stored data come into play? Is there something I need to do in the controller?
This code gives an undefined method 'account_type' error.
validates_format_of :name, :with => /^[a-z0-9_]+$/i, :on => :create if account_type == 2
If I u开发者_JS百科se a symbol then it doesn't give an error, but a symbol will never equal 2
validates_format_of :name, :with => /^[a-z0-9_]+$/i, :on => :create if :account_type == 2
It's confusing that you can validate the format of a symbol (like :name above) when :name only seems to be a reference with nothing stored in it.
Thanks!
Check the format for :if
parameter.
You need to pass method name (symbol) or Proc:
validates_format_of :name, :with => /^[a-z0-9_]+$/i, :on => :create, :if => Proc.new { |m| m.account_type == 2 }
精彩评论