Hello all,
I’m in the beginning states of learning ruby on rails development and
have got to a part where I’m a little stuck! Basically writing a simple
blog application, just to try something completely different!
I have a page which displays the current blog entry, it’s comments and
at the bottom of the page, a form to add a comment. So far, so good.
It’ll add comments, list them etc. So I decided I need a validation
routine going on and used “validates_presence_of :name” in my comments
model. This is the problem I’m facing now. In past examples I’ve looked
at, for example when you’ve just scaffolded a page, the page seems to
submit back to itself if there’s an error and displays the error using
<% error_messages_for ‘post’ %>
However, the way this seems to be working which is the same method that
the “creating a weblog in 15 minutes” video uses, the page will never
return to itself and so cannot display the error. It simply says
“comment added” and posts not passing the validation routine will not be
added. So I changed the code and have got this going on -
def comment
@comment = Post.find(params[:id]).comments.create(params[:comment])
if @comment.save
flash[:notice] = “added your comment.”
redirect_to :action => “show”, :id => params[:title]
else
flash[:notice] = “not added your comment.”
render :action => “show”, :id => params[:title]
end
end
Now this works, but am unable to make use of error_messages for ‘post’
and unable to display the reason why the comment was not added (no name
was entered).
Help! Any suggestions how I go about this?
Thanks!
Alastair