OK, this has me stumped...I have a Posts controller and model and a Comments controller and model. Posts has a show method and a corresponding view that shows a post and its associated comments, which are rendered with a partial from comments (<%= render :partial => "comments/comment", :collection => @post.comments %>). Another partial from comments is used to allow the user to add their comment to the page (<%= render :partial => "comments/new"%>). Problem: If a user tries to add a new comment but it fails the comment model validation, how do I get back to the show page from posts and display the model validation error for the new comment? controllers/comments_controller.rb ... def create @comment = Comment.new(params[:comment]) if @comment.save redirect_to :controller => "posts", :action => "show", :id => @comment.post else #we''re over in the comments controller - how do we get our data validation #errors from the comments model back over to the show page of posts? render :controller => "posts", :action => "show" #??? end -- 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-/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 -~----------~----~----~----~------~----~------~--~---
How about "redirect_to :back" ? That sends you back to the referer. On 8 Jan., 14:53, Yink Yank <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> OK, this has me stumped...I have a Posts controller and model and a > Comments controller and model. Posts has a show method and a > corresponding view that shows a post and its associated comments, which > are rendered with a partial > from comments (<%= render :partial => "comments/comment", :collection => > @post.comments %>). Another partial from comments is used to allow the > user > to add their comment to the page (<%= render :partial => > "comments/new"%>). > > Problem: If a user tries to add a new comment but it fails the comment > model validation, how do I get back to the show page from posts and > display the model validation error for the new comment? > > controllers/comments_controller.rb > ... > def create > @comment = Comment.new(params[:comment]) > if @comment.save > redirect_to :controller => "posts", :action => "show", :id => > @comment.post > else > #we''re over in the comments controller - how do we get our data > validation > #errors from the comments model back over to the show page of posts? > render :controller => "posts", :action => "show" #??? > end > -- > Posted viahttp://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-/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 -~----------~----~----~----~------~----~------~--~---
On Jan 8, 2008 5:53 AM, Yink Yank <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > OK, this has me stumped...I have a Posts controller and model and a > Comments controller and model. Posts has a show method and a > corresponding view that shows a post and its associated comments, which > are rendered with a partial > from comments (<%= render :partial => "comments/comment", :collection => > @post.comments %>). Another partial from comments is used to allow the > user > to add their comment to the page (<%= render :partial => > "comments/new"%>). > > Problem: If a user tries to add a new comment but it fails the comment > model validation, how do I get back to the show page from posts and > display the model validation error for the new comment? > > controllers/comments_controller.rb > ... > def create > @comment = Comment.new(params[:comment]) > if @comment.save > redirect_to :controller => "posts", :action => "show", :id => > @comment.post > else > #we''re over in the comments controller - how do we get our data > validation > #errors from the comments model back over to the show page of posts? > render :controller => "posts", :action => "show" #??? > endrender :action => ''posts/show'' ? I''m not sure if that works though. I''d actually create create a comments/new page for this purpose. render :action => ''new'' -- Rick Olson http://lighthouseapp.com http://weblog.techno-weenie.net http://mephistoblog.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-/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 -~----------~----~----~----~------~----~------~--~---
Rick Olson wrote:> On Jan 8, 2008 5:53 AM, Yink Yank <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> >> @comment.post >> else >> #we''re over in the comments controller - how do we get our data >> validation >> #errors from the comments model back over to the show page of posts? >> render :controller => "posts", :action => "show" #??? >> end > > render :action => ''posts/show'' ? I''m not sure if that works though. > I''d actually create create a comments/new page for this purpose. > > render :action => ''new'' > > -- > Rick Olson > http://lighthouseapp.com > http://weblog.techno-weenie.net > http://mephistoblog.comThe new comment form is a partial that belongs to the comments controller. It''s called by the show view of the posts controller. Apparently you can''t render a view from another controller in the way you suggest. And creating a stand-alone new comments page defeats the purpose because I want the post details, existing comments, and the new comment form to all be on one page. And if I redirect back to the show action of the posts controller from the comments controller, the posts controller will of course have forgotten about any of the data validation errors on the model controller. Probably there''s a way to pass the comment model errors as a param but this is starting to feel like a hack - funny because I moved from one "mega" controller in my original design to three controllers in an attempt to improve things! But it kind of feels like I''m working against how Rails wants things done here... -- 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-/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 -~----------~----~----~----~------~----~------~--~---