Sam Donaldson
2006-May-08 22:15 UTC
[Rails] link_to - Going Back without knowing the action name.
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. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060508/0cbd3696/attachment.html
Jeremy Kemper
2006-May-08 23:21 UTC
[Rails] link_to - Going Back without knowing the action name.
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
robert
2006-Jun-27 22:00 UTC
[Rails] Re: link_to - Going Back without knowing the action name.
> # Link back to where we came. > def link_back(name, fallback_options = {}, html_options = nil) > link_to name, url_back(fallback_options), html_options > endI tried this and am getting an "undefined method `link_to'' . . ." error when I try this. Any suggestions? -- Posted via http://www.ruby-forum.com/.
robert
2006-Jun-27 22:02 UTC
[Rails] Re: link_to - Going Back without knowing the action name.
> # Link back to where we came. > def link_back(name, fallback_options = {}, html_options = nil) > link_to name, url_back(fallback_options), html_options > endI tried this and am getting an "undefined method `link_to'' . . ." error. Any suggestions? -- Posted via http://www.ruby-forum.com/.
shai
2006-Jun-28 06:45 UTC
[Rails] Re: link_to - Going Back without knowing the action name.
@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 -- Posted via http://www.ruby-forum.com/.