Sunday, August 8, 2010

[Rails] Re: Validation problem

RichardOnRails wrote:
> Hi All,
>
> I've got an Expense model as follows:
>
> class Expense < ActiveRecord::Base
> belongs_to :vendor
>
> def validate_on_create
> unless params["expense"]["vendor_id"] != "0" # <= Line 5
> errors.add("A vendor must be selected before creating the
> expense record")
> end
> end
> end
>
> that crashes with:
>
> undefined local variable or method `params' for #<Expense:0x4716c00>
> [snip]

Your model by itself knows nothing about params received in the
controller. And by the time you're looking at validate_on_create, the
attributes from params have been assigned to the model, IIRC.

What about simply

def validate_on_create
unless vendor_id != 0
errors.add(blah blah blah)
end
end
--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate