I’m working on my first rails project and i’ve built a small site using rails and now i’m trying to follow the getting started with rails tutorial to build a blog.
I’m stuck on 6.5 of http://guides.rubyonrails.org/getting_started.html when trying to validate my blog post and this is the error that I get:Unknown validator: 'PresencesValidator'.
Here’s my post model
class Post < ActiveRecord::Base
validates :name, :presence => { :message => "Name cannot be blank" }
validates :title, :presence => { :message => "Title cannot be blank" }
validates :content, :presences => { :message => "Content cannot be blank" }
end
I’ve also tried just setting :presence => true, but same error.
Why am i getting this error and how do i fix it?
Because this is a typo:
You wrote
presenceswhen you meant to writepresence.Also, the message you’ve given on these is the default, so you actually don’t need to provide it.
One last thing, if you want to validate the presence of three things you could do it in one line via:
That would accomplish the same exact behavior as the code you were trying to write. There are two validation syntaxes, the one you’ve been using is better for when you have a lot of validations on each attribute, I personally think the other kind is better when you have many different attributes you want to run the same validation on. Basically whichever requires less typing.
Take a look at the Validations and Callbacks guide as a reference. Cheers.