Hello all,
I am trying to make “Back” link functionality work in my app.
Basically the referring page has a list of items and on clicking I
display the details on the items. But when the user clicks “Back”, the
user should go back to the list display. I would also like to preserve
the pagination in the referring page display meaning go back to the
same page because the user would hate to start all over again and
again after going through a couple of pages of items…
Is there a way to do it? I found the following two solutions in the
previous forum postings. First one didn’t work for me. Second one
worked but couldn’t preserve the page number of the referrer. So the
user goes back to the first page of list display. I am posting both
the suggested solutions for your reference. I would greatly appreciate
your insights and comments.
Solution #1
def back
redirect_to :back
end
then i’d add the following helper to your application_helper
def link_back(text=‘back’)
link_to text, :controller => ‘welcome’, :action => ‘back’
end
and then you can use it in your views as:
<%= link_back ‘go back where you came from’ %>
Solution #2
session[:cache] ||= []
session[:cache] << params
def back
if session[:cache] && last_params = session[:cache].pop
redirect_to last_params
end
end