Forwarding between controllers

Hi

I am a little confused - perhaps because of coming at this with a Java head but..

I am looking for the equivalent of a forward from Java MVC frameworks whereby the request and it's model data contents can be forwarded to another controller ?

Required scenario :

- NewsController posts to CommentsController to add a comment. - If the comment is valid we REDIRECT back to NewsContoller.show, all good. - If the comment is invalid we FORWARD back to NewsContoller.show. The comment (and error msgs) would still be in request scope so that view can render appropriate hints.

I currently get round this by using redirect_to (in place of a forward) and sticking the comment in the session. Then, in NewsController.show i check the session before loading the comment from the DB. I think this is kinda sucky but I found a DHH post that said there are no kinda forwards in rails :

http://groups.google.com/group/rubyonrails-talk/browse_frm/thread/e8fd7e7953d2350b/7e101906c5869e59?lnk=gst&q=forward&rnum=3#7e101906c5869e59

How do peeps approach this scenario ?

Thanks

Matt

Yea, I've seen render and thought of this approach.

Things is, I'd need to have an add_comment on all *Controllers that have comments.

That's why I wanted to (re)use CommentsController.create

Perhaps Rails just isn't meant to work like this ?

Any thoughts ?

Matt

Wai, thanks for the suggestions (and your patience !).

I don't think filters are what I'm after and the validation is already within my model class (Comment)..

I guess this is coming down to a stylistic thing (and, as I said, perhaps a hangup from Java MVC) but in my understanding, controllers are the exposed facade for CRUD style actions. All I am really asking is why one controller can't use another in the same request scope ?

> - NewsController posts to CommentsController to add a comment.

BTW, is there any particular reason why the NewsController would post to another Controller instead of a Model?

Badly worded on my part, sorry. What I mean is the HTML form, rendered by a NewsController.show, POSTs to CommentsController.create (actually just via "/comments" with a POST and the ActiveResource stuff takes care of the mapping to CommentsController.create).

The comments form is wrapped up in a partial that I can reuse in any view (say, Interview). Which would also POST to CommentsController.create..

The comments model (acts_as_commentable) has the context and the id of it's creator ([NEWS,1,"a comment","matt"],[NEWS,2,"another comment", "fred"], [INTERVIEW,1,"great interview","john"]). So thats all well and good. And, upon a successful creation, CommentsController redirects back to it's caller (News or Interviews). It's just when not successful, I'd like to FORWARD back to the caller without new request scope or session munging.

Perhaps the code may help (CommentsController.create):

Grrr - google groups keeps erroring !

Already tried to reply to this, here we go again.

I just realize I misinterpretated what you want to do in the create method. You can use

render :controller => @comment.commentable_type, :action => @comment.commentable_id

to render the page without going out of the request scope, until the comment is valid (then use redirect_to).

"render" doesn't take a :controller symbol so it looks like I am stuck with a "redirect" and chaining the Comment model into the session for now..

AFAIK "render" only works for actions within the current controller. Yes you can use partial templates but that's a different usage.

Thanks anyway..

"render" doesn't take a :controller symbol so it looks like I am stuck with a "redirect" and chaining the Comment model into the session for now..

AFAIK "render" only works for actions within the current controller. Yes you can use partial templates but that's a different usage.

  You want render_component instead, it listens to :controller ... the documentation says it's slower, tho.

  - Tyler

@Bharat - Got the book next to the laptop :slight_smile: Doesn't cover such a scenario tho..

Its seems (as I suspected) that this concept just doesn't exist in Rails, especially considering the DHH response (link in my first post).

All the methods suggested are about reusing view templates in the context of the current controller. Yes, I can use "render" and reference another template but this is in the _context_ of the same controller (and URL), you'd have to be sure to set up the correct instance variables for the view also (which required some more dynamic coding in my case).

My initial workaround is still used in my code and works fine. I'd just rather not be doing it that way.

I think Rails would rather I just put an "add_comments" entry point in each controller utilising comments rather than form submitting to the CommentsController.create. Perhaps, just a stylistic thing..

If I am wrong, I'd love to be told. But please, make sure you understand this thread !

Cheers

Matt