Show Validation Errors after redirecting

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 :slight_smile: 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

Typical newbie trap here :slight_smile:

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.

Hi,

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.

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.

Thank you both! I use render :template => "articles/show" now, I didn't know that's possible.

Greetings Christoph