chriskuffo
(Chris Kuffo)
1
I created a simple blog application and wanted to enforce a rule that every post must have a title. In my post.rb file I have this code
class Post < ActiveRecord::Base
attr_accessible :body, :title
validates_presence_of :body, :title
end
When I update my post and delete my title I do not receive an error.
patrikbona
(Patrik Bóna)
2
Start rails console
and type:
Post.new.valid?
What is the output?
Edit:
What is the exact problem? You just do not receive any error, or post is saved without title?
If you need to show errors then you need to add something like this to your view:
<% if @post.errors.any? %>
<div class="alert alert-error alert-block">
Some errors occured:
<ul>
<% @post.errors.full_messages.each do |error| %>
<li><%= error %></li>
<% end %>
</ul>
</div>
<% end %>
chriskuffo
(Chris Kuffo)
3
The output was false… I was able to resolve my problem though…seems liked I placed
validates_presence_of
in the wrong file. now i receive an error advising me that every post needs something in the body and title.