Forum: Ruby on Rails link_to - Going Back without knowing the action name.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
036be8d5d83a98e546b98d64e3fd6c20?d=identicon&s=25 Sam Donaldson (Guest)
on 2006-05-09 00:16
(Received via mailing list)
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.
24d2f8804e6bb4b7ea6bd11e0a586470?d=identicon&s=25 Jeremy Kemper (Guest)
on 2006-05-09 01:23
(Received via mailing list)
On May 8, 2006, at 3:15 PM, Sam Donaldson 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
B11d817333874136bb4b36222b73fc08?d=identicon&s=25 robert (Guest)
on 2006-06-27 23:59
>      # 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?
B11d817333874136bb4b36222b73fc08?d=identicon&s=25 robert (Guest)
on 2006-06-27 23:59
>      # 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?
0a85dcd26e7b70af1c8039bfc49b23c5?d=identicon&s=25 shai (Guest)
on 2006-06-28 08:45
      @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
This topic is locked and can not be replied to.