# How to handle my comment validation errors

**URL:** <https://www.ruby-forum.com/t/how-to-handle-my-comment-validation-errors/178549>\
**Category:** Rails\
**Created:** [November 6, 2009, 10:32am UTC](https://www.ruby-forum.com/t/how-to-handle-my-comment-validation-errors/178549 "2009-11-06T10:32:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [November 6, 2009, 10:32am UTC](https://www.ruby-forum.com/t/how-to-handle-my-comment-validation-errors/178549/1 "2009-11-06T10:32:24Z")

</div>

I’ve got a resorts view, shows details of a ski resort. Under the resort  
description I allow users to leave comments on the resort - works fine.

- Resort has many comments.
- Comment belongs to resort.

However, slight problemo - the comment form on my resorts page creates a  
new comment via the Comments Controller (new and create actions are  
available to the public). This works fine.

But how oh how do I send back the comment validation errors to the  
Resorts show view when redirected from the comments controller - I’d  
like to do this.

It’s horrible at the moment - I’m using the flash to display feedback to  
the user, it’s not ideal.

How should I handle this - surely a common situation?

When I redirect back to the resort can I pass back the comment complete  
with any errors?

bb

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [November 6, 2009, 11:52am UTC](https://www.ruby-forum.com/t/how-to-handle-my-comment-validation-errors/178549/2 "2009-11-06T11:52:09Z")

</div>

Basically you can see what I followed here.

> **[How to write a blog in Rails 2.0.2 (not quite in 15 minutes)](https://skionrails.wordpress.com/tutorials/how-to-write-a-blog-in-15-minutes/)**
>
> Like many people, the “Creating a weblog in 15 minutes” screencast by DHH is what convinced me to try Rails. Since I have never found a version of this tutorial that works for Rails 2, …

My comments on my resorts in this case are similar to the comments on  
each blog post.

But when this guy redirects back to the @post I can’t see how validation  
errors can be included?  
He just uses the flash?

Please advise.

Thanks

Ruert

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [November 6, 2009, 12:56pm UTC](https://www.ruby-forum.com/t/how-to-handle-my-comment-validation-errors/178549/3 "2009-11-06T12:56:18Z")

</div>

Here’s my existing comments controller create action…

def create  
@resort = Resort.find(params[:resort\_id])  
@comment = Comment.new(params[:comment])  
@comment.resort = @resort  
if @comment.save  
flash[:notice] = “Successfully created comment, it’ll be cleared  
and show up soon on this page.”  
else  
flash[:notice] = “Something went wrong creating your comment.”  
end  
redirect\_to @resort  
end

As you can see, as it stands the second to last line just redirects.

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [November 7, 2009, 6:06pm UTC](https://www.ruby-forum.com/t/how-to-handle-my-comment-validation-errors/178549/4 "2009-11-07T18:06:43Z")

</div>

Either you render the resort show action if your create fails or you  
use ajax to update only the comment part of your view.

2009/11/6, bingo bob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> with any errors?
> 
> ## bb
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Von meinen Mobilgerät aus gesendet
