Forum: Ruby on Rails validation errors bring up stack trace error page

Posted by Ze Ca (closedbook)
on 2010-09-01 04:35
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!
Posted by Colin Law (Guest)
on 2010-09-01 09:09
(Received via mailing list)
On 1 September 2010 03:35, Ze Ca <lists@ruby-forum.com> 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
Posted by Ze Ca (closedbook)
on 2010-09-01 13:48
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:
> On 1 September 2010 03:35, Ze Ca <lists@ruby-forum.com> 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
Posted by Frederick Cheung (Guest)
on 2010-09-01 14:26
(Received via mailing list)
On Sep 1, 12:48 pm, Ze Ca <li...@ruby-forum.com> 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
Posted by Ze Ca (closedbook)
on 2010-09-02 04:04
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:
> On Sep 1, 12:48�pm, Ze Ca <li...@ruby-forum.com> 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
Posted by Frederick Cheung (Guest)
on 2010-09-02 09:43
(Received via mailing list)
On Sep 2, 3:04 am, Ze Ca <li...@ruby-forum.com> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.