# Navigation - Link back to a calling controller/action

**URL:** <https://www.ruby-forum.com/t/navigation-link-back-to-a-calling-controller-action/49624>\
**Category:** Rails\
**Created:** [December 7, 2005, 1:33am UTC](https://www.ruby-forum.com/t/navigation-link-back-to-a-calling-controller-action/49624 "2005-12-07T01:33:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nfstern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ba8739/32.png) [@nfstern](https://www.ruby-forum.com/u/nfstern)\
**Post date:** [December 7, 2005, 1:33am UTC](https://www.ruby-forum.com/t/navigation-link-back-to-a-calling-controller-action/49624/1 "2005-12-07T01:33:22Z")

</div>

I’m a newbie to Ruby on Rails and I have what I think is a fairly simple  
question.

I’m developing an application which lists Enterprise Services. Each  
Service has a User (actually a contact person) which I display with a  
link to the Users show view.

I got this working from the Services List screen as follows:

 \<%= link\_to service.user.userid, { :controller =\> "users", :action =\> "show", :id =\> service.user\_id, :lastcontroller =\> "services", :lastaction =\> "list" } %\> - \<%= service.user.firstname %\> \<%= service.user.lastname %\>

My problem is that I cannot figure out the syntax to get the back link  
on the Users show screen working.

My users\_controller.rb has this:  
def show  
last\_controller = @params[‘lastcontroller’]  
last\_action = @params[‘lastaction’]

```
@user = User.find(@params['id'])

```

end

I tried variations of this in my show.rhtml and got errors.  
\<%= link\_to ‘Back’, :controller =\> lastcontroller, :action =\> lastaction  
%\>

Any suggestions would be greatly appreciated.

Thx  
nfstern

---

<div class="post-metadata">

**Author:** ![nfstern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ba8739/32.png) [@nfstern](https://www.ruby-forum.com/u/nfstern)\
**Post date:** [December 7, 2005, 8:04am UTC](https://www.ruby-forum.com/t/navigation-link-back-to-a-calling-controller-action/49624/2 "2005-12-07T08:04:11Z")

</div>

Hey Noah,

Unless you do not really want to use JS, you could use:

\<%= link\_to ‘Back’, ‘javascript:history.go(-1)’ %\>

HTH,

Deepak

---

<div class="post-metadata">

**Author:** ![nfstern](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ba8739/32.png) [@nfstern](https://www.ruby-forum.com/u/nfstern)\
**Post date:** [December 7, 2005, 3:20pm UTC](https://www.ruby-forum.com/t/navigation-link-back-to-a-calling-controller-action/49624/3 "2005-12-07T15:20:28Z")

</div>

ncdram wrote:

> Hey Noah,
> 
> Unless you do not really want to use JS, you could use:
> 
> \<%= link\_to ‘Back’, ‘javascript:history.go(-1)’ %\>
> 
> HTH,
> 
> Deepak

Okay thanks Deepak. I did eventually get it to work using 2 instance  
variables @last\_controller & @last\_action. However this only works when  
you go 1 screen back. If you do this sequence: List Services -\> Show  
User -\> Edit User, it breaks trying to go back from Edit to Show. I  
finally decided that if the user wants to go back, they can click on the  
back button on Firefox, so I disabled the back link.

I think your solution probably fixes that and I will try it when I get  
into the office today.
