Link_to - Going Back without knowing the action name


#1

Hi,

I have a bunch of link_to’s in my code that are simple Back buttons that
should basically just go to the previous request. Is there a way of
doing
this without specifying the action to execute? The problem is,
sometimes I
may render the same view through separate actions so I wouldn’t know
which
action to go back to.

Thanks.


#2

On May 8, 2006, at 3:15 PM, Sam D. wrote:

I have a bunch of link_to’s in my code that are simple Back buttons
that should basically just go to the previous request. Is there a
way of doing this without specifying the action to execute? The
problem is, sometimes I may render the same view through separate
actions so I wouldn’t know which action to go back to.

Consider passing a :return_to parameter or session variable along
with helper methods to manage your back links. For example:

class ApplicationController < ActionController::Base
helper_method :url_back, :link_back

protected
# Try params[:return_to] and session[:return_to].
def url_back(options = {})
params[:return_to] or session[:return_to] or options
end

 # Redirect back to where we came.
 def redirect_back(options = {})
   redirect_to(url_back)
 end

 # Link back to where we came.
 def link_back(name, fallback_options = {}, html_options = nil)
   link_to name, url_back(fallback_options), html_options
 end

end

Best,
jeremy


#3
 # Link back to where we came.
 def link_back(name, fallback_options = {}, html_options = nil)
   link_to name, url_back(fallback_options), html_options
 end

I tried this and am getting an “undefined method `link_to’ . . .” error
when I try this. Any suggestions?


#4
 # Link back to where we came.
 def link_back(name, fallback_options = {}, html_options = nil)
   link_to name, url_back(fallback_options), html_options
 end

I tried this and am getting an “undefined method `link_to’ . . .” error.
Any suggestions?


#5
  @session['return-to'] = @request.request_uri
  # as apposed to redirect to \ redirect to URL
  redirect_to_url @session['return-to']
  #to reset the session-return-to
  @session['return-to'] = nil

haven’t checked this, but i hope it works/helps,

shai