# Validation errors bring up stack trace error page

**URL:** <https://www.ruby-forum.com/t/validation-errors-bring-up-stack-trace-error-page/194208>\
**Category:** Rails\
**Created:** [September 1, 2010, 4:35am UTC](https://www.ruby-forum.com/t/validation-errors-bring-up-stack-trace-error-page/194208 "2010-09-01T04:35:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [September 1, 2010, 4:35am UTC](https://www.ruby-forum.com/t/validation-errors-bring-up-stack-trace-error-page/194208/1 "2010-09-01T04:35:39Z")

</div>

hello!

I made some custom validations for my app, which do catch invalid data  
in the console. However, when I enter invalid data through a form on the  
browser view, I get your stack trace error style page (similar to what  
you see when you get a syntax error or a nil object) instead of the  
nicely styled error messages (what you would see in a scaffolded  
application.

First off, what is the stack trace error screen called in rails? And  
secondly, why can’t I style the error?

view code:

\<% form\_for [@post, Comment.new] do |f| %\>

\<%= f.label :title, "Your Comment" %\>  
 \<%= f.error\_messages %\> \<%= f.text\_field :title %\>

 \<%= f.submit "Submit Your Comment" %\>

controller code:

class CommentsController \< ApplicationController  
def create  
@post = Post.find(params[:post\_id])  
@comment = @post.comments.create!(params[:comment])  
redirect\_to @post  
end  
end

Thanks!

---

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [September 1, 2010, 9:09am UTC](https://www.ruby-forum.com/t/validation-errors-bring-up-stack-trace-error-page/194208/2 "2010-09-01T09:09:44Z")

</div>

On 1 September 2010 03:35, Ze Ca [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> secondly, why can’t I style the error?  
> Can you provide an example of what you mean? If you are getting a  
> runtime Ruby error then you should never allow that to happen in your  
> code, so there is no need to style it. If for example a variable may  
> be nil then you must test for nil before using it. Perhaps I  
> misunderstand what you mean however.

Colin

---

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [September 1, 2010, 1:48pm UTC](https://www.ruby-forum.com/t/validation-errors-bring-up-stack-trace-error-page/194208/3 "2010-09-01T13:48:07Z")

</div>

Thanks Colin for your response.

The error _looks_ like a runtime error, however the error message that  
is given is:

ActiveRecord::RecordInvalid in CommentsController#create

Validation failed: Sorry, that’s an invalid move.

which tells me the validation is working, however what I want is to  
style it in a way that gives off a red warning directly on the page.

Thanks!

Colin L. wrote:

> On 1 September 2010 03:35, Ze Ca [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > secondly, why can’t I style the error?  
> > Can you provide an example of what you mean? If you are getting a  
> > runtime Ruby error then you should never allow that to happen in your  
> > code, so there is no need to style it. If for example a variable may  
> > be nil then you must test for nil before using it. Perhaps I  
> > misunderstand what you mean however.
> 
> Colin

---

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [September 1, 2010, 2:26pm UTC](https://www.ruby-forum.com/t/validation-errors-bring-up-stack-trace-error-page/194208/4 "2010-09-01T14:26:29Z")

</div>

On Sep 1, 12:48 pm, Ze Ca [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> style it in a way that gives off a red warning directly on the page.

typically you do something like

if Foo.create(params[…])  
#handle success  
else  
render … #re render the form used to submit the object  
end

If you do this the object that failed validation is still around, so  
you can prefill edit fields and so on with what the user has just  
tried and either display the errors yourself or use  
error\_messages\_for / f.error\_messages

Fred

---

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [September 2, 2010, 4:04am UTC](https://www.ruby-forum.com/t/validation-errors-bring-up-stack-trace-error-page/194208/5 "2010-09-02T04:04:31Z")

</div>

Fred,

Thanks for the recommendation. I added the code below to my comments  
controller:

def create  
@post = Post.find(params[:post\_id])  
@comment = @post.comments.create!(params[:comment])  
respond\_to do |format|  
if @comment.create(params[:comment])  
format.html { redirect\_to(@post) }  
format.xml { render :xml =\> @post, :status =\> :created,  
:location =\> @post }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @post.errors, :status =\>  
:unprocessable\_entity }  
end  
end  
end

I’m still not getting the errors to show up in red on the form. This is  
a comment form within the post “show” view, which might be different?  
The errors show in red when I’m creating a new game, but I get the  
runtime error when making a new invalid comment.

Thanks!

Frederick C. wrote:

> On Sep 1, 12:48ï¿½pm, Ze Ca [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > style it in a way that gives off a red warning directly on the page.
> 
> typically you do something like
> 
> if Foo.create(params[…])  
> #handle success  
> else  
> render … #re render the form used to submit the object  
> end
> 
> If you do this the object that failed validation is still around, so  
> you can prefill edit fields and so on with what the user has just  
> tried and either display the errors yourself or use  
> error\_messages\_for / f.error\_messages
> 
> Fred

---

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [September 2, 2010, 9:43am UTC](https://www.ruby-forum.com/t/validation-errors-bring-up-stack-trace-error-page/194208/6 "2010-09-02T09:43:22Z")

</div>

On Sep 2, 3:04 am, Ze Ca [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m still not getting the errors to show up in red on the form. This is  
> a comment form within the post “show” view, which might be different?  
> The errors show in red when I’m creating a new game, but I get the  
> runtime error when making a new invalid comment.

you’re still calling create! rather than create.

Fred
