djburdick
djburdick

Reputation: 11950

ruby on rails: How do I access variable data in a url parameter passed to a model?

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!

Upvotes: 1

Views: 252

Answers (1)

Tatjana N.
Tatjana N.

Reputation: 6225

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 }

Upvotes: 1

Related Questions