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 via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 1 September 2010 03:35, Ze Ca <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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> > 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 >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Sep-01 12:12 UTC
Re: validation errors bring up stack trace error page
On Sep 1, 12:48 pm, Ze Ca <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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. >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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Sep-02 07:42 UTC
Re: validation errors bring up stack trace error page
On Sep 2, 3:04 am, Ze Ca <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.