validation errors bring up stack trace error page

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| %>

  <p>     <%= f.label :title, "Your Comment" %><br />     <%= f.error_messages %>     <%= f.text_field :title %><br />   </p>   <%= 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!

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?

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

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 Law wrote:

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

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 Cheung wrote:

you're still calling create! rather than create.

Fred