开发者

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

开发者 https://www.devze.com 2022-12-29 07:58 出处:网络
I have a variable called \"account_type\" passed from a rails form and need to access the value in the corresponding 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 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 }
0

精彩评论

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

关注公众号