# MVC Blues - Who's In Control Here Anyway?

**URL:** <https://www.ruby-forum.com/t/mvc-blues-whos-in-control-here-anyway/125356>\
**Category:** Rails\
**Created:** [January 8, 2008, 2:53pm UTC](https://www.ruby-forum.com/t/mvc-blues-whos-in-control-here-anyway/125356 "2008-01-08T14:53:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yinkyank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d9b06d/32.png) [@yinkyank](https://www.ruby-forum.com/u/yinkyank)\
**Post date:** [January 8, 2008, 2:53pm UTC](https://www.ruby-forum.com/t/mvc-blues-whos-in-control-here-anyway/125356/1 "2008-01-08T14:53:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yinkyank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d9b06d/32.png) [@yinkyank](https://www.ruby-forum.com/u/yinkyank)\
**Post date:** [January 8, 2008, 4:14pm UTC](https://www.ruby-forum.com/t/mvc-blues-whos-in-control-here-anyway/125356/2 "2008-01-08T16:14:21Z")

</div>

How about “redirect\_to :back” ?  
That sends you back to the referer.

---

<div class="post-metadata">

**Author:** ![yinkyank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d9b06d/32.png) [@yinkyank](https://www.ruby-forum.com/u/yinkyank)\
**Post date:** [January 8, 2008, 4:43pm UTC](https://www.ruby-forum.com/t/mvc-blues-whos-in-control-here-anyway/125356/3 "2008-01-08T16:43:40Z")

</div>

On Jan 8, 2008 5:53 AM, Yink Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![yinkyank](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d9b06d/32.png) [@yinkyank](https://www.ruby-forum.com/u/yinkyank)\
**Post date:** [January 8, 2008, 5:12pm UTC](https://www.ruby-forum.com/t/mvc-blues-whos-in-control-here-anyway/125356/4 "2008-01-08T17:12:57Z")

</div>

Rick O. wrote:

> On Jan 8, 2008 5:53 AM, Yink Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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://lighthouseapp.com)  
> [http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
> [http://mephistoblog.com](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…
