Navigation - Link back to a calling controller/action


#1

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


#2

Hey Noah,

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

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

HTH,

Deepak


#3

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.