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 :) 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 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Typical newbie trap here :) 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. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi, On 18 Jul 2008, at 09:56, Christoph wrote:> 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. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> > > > 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. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thank you both! I use render :template => "articles/show" now, I didn''t know that''s possible. Greetings Christoph On 18 Jul., 12:45, Tony Byrne <tonyby...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > On 18 Jul 2008, at 09:56, Christoph wrote: > > > 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.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---