MVC Blues - Who's In Control Here Anyway?

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

How about “redirect_to :back” ?
That sends you back to the referer.

On Jan 8, 2008 5:53 AM, Yink Y. [email protected]
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 O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com

Rick O. wrote:

On Jan 8, 2008 5:53 AM, Yink Y. [email protected]
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 O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com

The 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…