# Show Validation Errors after redirecting

**URL:** <https://www.ruby-forum.com/t/show-validation-errors-after-redirecting/143419>\
**Category:** Rails\
**Created:** [July 18, 2008, 10:57am UTC](https://www.ruby-forum.com/t/show-validation-errors-after-redirecting/143419 "2008-07-18T10:57:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [July 18, 2008, 10:57am UTC](https://www.ruby-forum.com/t/show-validation-errors-after-redirecting/143419/1 "2008-07-18T10:57:07Z")

</div>

Hi all,  
I’ve got another newbie question. I have Article and Comment models/  
controllers, and users can comment articles, this should be pretty  
clear 🙂  
Here is some code:  
class CommentsController \< ApplicationController  
before\_filter :load\_article  
require\_role :member, :for =\> :comment  
def create  
@comment = Comment.new(params[:comment])  
@comment.user, @comment.article = current\_user, @article

```
respond_to do |wants|
  if @comment.save
    flash[:notice] = 'Comment was successfully created.'
    wants.html { redirect_to(@article) }
  else
    wants.html { redirect_to(@article) }
  end
end

```

end

protected  
def load\_article  
@article = Article.find\_by\_stripped\_title(params[:article\_id])  
end  
end

After creating a new comment, the user gets redirected to the “show”  
article page. If there are any errors, they wouldn’t be shown (?).  
Maybe can I store the @comment.errors object in the flash, but how can  
I display them nicely formatted (like the default scaffold errors) on  
my page ? My current show.html.haml template looks like this:  
%fieldset.clear  
%legend  
Add your comment now

- form\_for :comment, :url =\> article\_comments\_path(@article) do |f|  
= f.error\_messages  
%div  
= f.label :title, “Title”  
= f.text\_field :title  
%div  
= f.label :body, “Comment”  
= f.text\_area :body, :value =\> “Your comment here…”  
%div  
= f.submit “Submit”, :disable\_with =\> ‘Submiting…’
- end

Thank you very much for you ideas,  
Greetings  
Christoph

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [July 18, 2008, 11:41am UTC](https://www.ruby-forum.com/t/show-validation-errors-after-redirecting/143419/2 "2008-07-18T11:41:42Z")

</div>

Typical newbie trap here 🙂

A redirect tells your users browser to request a new page!  
So there is no way to store anything in flashs or whatever.  
The mongrel that processed the first request is gone and  
something new started.

Use  
render :action =\> :show  
instead of a redirect  
or  
render :template =\> “articles/show”

or something on that line. For a redirect the only to store  
something is the session and most likely you don’t want  
to trash it with error messages.

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [July 18, 2008, 4:14pm UTC](https://www.ruby-forum.com/t/show-validation-errors-after-redirecting/143419/3 "2008-07-18T16:14:50Z")

</div>

> A redirect tells your users browser to request a new page!  
> So there is no way to store anything in flashs or whatever.  
> The mongrel that processed the first request is gone and  
> something new started.

I don’t believe this is a good enough explanation for a beginner to  
understand. You certainly can use the flash between requests. I think  
you  
mean instance variables.

Also the “mongrel that processed the first request” isn’t gone. The  
request  
ends, but Mongrel is the server and it doesn’t just go away. Your next  
request could be served by a different Mongrel server if the setup is  
load-balanced that way, but your sessions should still be secure because  
you’ve used a distributed session storage model like ActiveRecord store  
or  
Cookie store.

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [July 18, 2008, 12:46pm UTC](https://www.ruby-forum.com/t/show-validation-errors-after-redirecting/143419/4 "2008-07-18T12:46:40Z")

</div>

Hi,

On 18 Jul 2008, at 09:56, Christoph wrote:

> After creating a new comment, the user gets redirected to the “show”  
> article page. If there are any errors, they wouldn’t be shown (?).  
> Maybe can I store the @comment.errors object in the flash, but how can  
> I display them nicely formatted (like the default scaffold errors) on  
> my page ? My current show.html.haml template looks like this:

Instead of redirecting, can you not just leave them on the comment  
page when there are  
validation errors? This allows Rails to do the heavy lifting for you  
as well as affording  
the user the opportunity to fix the error and resubmit the comment.

Regards,

Tony.

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [July 18, 2008, 4:31pm UTC](https://www.ruby-forum.com/t/show-validation-errors-after-redirecting/143419/5 "2008-07-18T16:31:48Z")

</div>

Thank you both!  
I use render :template =\> “articles/show” now, I didn’t know that’s  
possible.

Greetings  
Christoph
