How implement the Back Link to the page from which

2019-09-09 22:06发布

问题:

I have an application with associations and will pagination the pages.

The index page from the main object "cat_list" shows links to the association "data_lists". The index page has also pagination with "will_paginate"

  1. I show for example page=3 "/cat_lists?page=3"
  2. I click the link of a "data_lists" for example "/cat_lists/8984/data_lists"

This index page shows a list of data_lists with Edit, Destroy and a New link. And a Back Link to the cat_lists index page now "/cat_lists"

What is the best practice to implement the features, that the Back Link now the page from which comes from?

回答1:

I usually record the history in the session and then call it via redirect_to back (no colon)

def index
  ... do your stuff ...
  record_history
end

protected

  def record_history
    session[:history] ||= []
    session[:history].push request.url
    session[:history] = session[:history].last(10) # limit the size to 10
  end

  def back
    session[:history].pop
  end

Note that this only works for GET requests.



回答2:

If I understand you correctly link_to('Back', :back) is what you want.



回答3:

I also use mosch's approach.

link_to('Back', :back) only uses the browsers 'back' functionality. Managing the history server side gives you more control (i.e. if you've come from a google search, guess what happens on :back). Managing the history server side gives you the possibility to hide links that would take the user off your page. Further you can offer the user to browse multiple steps back - i.e. via dropdown.