# Best way to do redirect\_to using multiple controllers?

**URL:** <https://www.ruby-forum.com/t/best-way-to-do-redirect-to-using-multiple-controllers/126338>\
**Category:** Rails\
**Created:** [January 17, 2008, 3:35pm UTC](https://www.ruby-forum.com/t/best-way-to-do-redirect-to-using-multiple-controllers/126338 "2008-01-17T15:35:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [January 17, 2008, 3:35pm UTC](https://www.ruby-forum.com/t/best-way-to-do-redirect-to-using-multiple-controllers/126338/1 "2008-01-17T15:35:41Z")

</div>

I’m using the same comment system for two different controllers, so  
users post a comment to a game and a post using the same “create” action  
in the comments controller.

I did comments for games first, so my comments create action looks like  
this:

def create  
@comment = Comment.new(params[:comment])  
if @comment.save  
flash[:notice] = “You have successfully added a comment.”  
redirect\_to :controller =\> ‘games’, :action =\> ‘show’, :id =\>  
@comment.game.id  
else  
render :action =\> ‘new’  
end  
end

What’s the best way to tell this action where the comment was posted, so  
the redirect\_to sends them back to the correct post or game?

Thanks!

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [January 17, 2008, 3:50pm UTC](https://www.ruby-forum.com/t/best-way-to-do-redirect-to-using-multiple-controllers/126338/2 "2008-01-17T15:50:24Z")

</div>

On Jan 17, 2008, at 8:35 AM, Dave A. wrote:

> What’s the best way to tell this action where the comment was  
> posted, so  
> the redirect\_to sends them back to the correct post or game?

One thing you could do is put the destination info in the session,  
then later redirect like this:

redirect\_to :action =\> session[:back\_action], :controller =\>  
session[:back\_controller], :id =\> session[:back\_id]

And of course earlier some other action had done something like this:

```
 session[:back_action] = "show"
 session[:back_controller] = "games"
 session[:back_id] = @comment.game.id

```

(untested)

–  
-George

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [January 17, 2008, 8:09pm UTC](https://www.ruby-forum.com/t/best-way-to-do-redirect-to-using-multiple-controllers/126338/3 "2008-01-17T20:09:25Z")

</div>

> And of course earlier some other action had done something like this:
> 
> ```
> session[:back_action] = "show"
> session[:back_controller] = "games"
> session[:back_id] = @comment.game.id
> 
> ```
> 
> (untested)
> 
> –  
> -George

Thanks, that’s a good idea. The problem I’m having is that if I put that  
code in my controller, let’s say in the action where the user can create  
the comment, @comment doesn’t exist yet. But it’s a good start – I’ll  
see what I can figure out.

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [January 17, 2008, 9:59pm UTC](https://www.ruby-forum.com/t/best-way-to-do-redirect-to-using-multiple-controllers/126338/4 "2008-01-17T21:59:56Z")

</div>

Dave A. wrote:

> > And of course earlier some other action had done something like this:
> > 
> > ```
> > session[:back_action] = "show"
> > session[:back_controller] = "games"
> > session[:back_id] = @comment.game.id
> > 
> > ```
> > 
> > (untested)
> > 
> > –  
> > -George
> 
> Thanks, that’s a good idea. The problem I’m having is that if I put that  
> code in my controller, let’s say in the action where the user can create  
> the comment, @comment doesn’t exist yet. But it’s a good start – I’ll  
> see what I can figure out.

You could use a hidden field or (fields) in the forms.

\<%= hidden\_field\_tag ‘from\_controller’, ‘posts’ %\>  
\<%= hidden\_field\_tag ‘from\_id’, post\_id %\>

then in your controller…

redirect\_to :controller =\> params[:from\_controller], :action =\> ‘show’,  
:id =\> params[:from\_id]

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [January 18, 2008, 12:57am UTC](https://www.ruby-forum.com/t/best-way-to-do-redirect-to-using-multiple-controllers/126338/5 "2008-01-18T00:57:25Z")

</div>

> You could use a hidden field or (fields) in the forms.
> 
> \<%= hidden\_field\_tag ‘from\_controller’, ‘posts’ %\>  
> \<%= hidden\_field\_tag ‘from\_id’, post\_id %\>
> 
> then in your controller…
> 
> redirect\_to :controller =\> params[:from\_controller], :action =\> ‘show’,  
> :id =\> params[:from\_id]

Yes, that did it. Thanks!
