1

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 use 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!

1 Answer 1

1

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 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.