Forum: Ruby on Rails Going back doesn't always go, well, back.

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.
6f9418c7978f7f3116b70fc08682161f?d=identicon&s=25 Ken Wagner (beaucoder)
on 2008-12-19 08:08
Addendum to my prior entry.

Rails scaffold creates the index, show, edit, update, destroy, create
views.

But Rails does not provide a correct "back" method to get to the right
spot in the index listing. Example: Index view lists 387 lines of items
from SQL file. But using 'back' in show, edit, etc only goes back to TOP
of a new index.

Using <%= link_to 'Back', items_path %> ONLY goes back to top of index
view pg.
I.e, reloads it all over again

Browser button goes back to correct point in index, you see page exactly
where you were: top, bottom or WHEREVER IN BETWEEN.

'Redirect_to :back' Rails helper won't work with <%= link_to 'Back', ...
%>

Also, response.env['HTTP_REFERRER'] works like 'link_to' - redoes index
from top.

Must use Javascript:history.go(-1) in a link construct. <a ....>Back</a>
or the browser "Back" button/context menu choice.

Why can't rails just correctly go back one page with 'redirect_to
:back'?

As it is 'Back' in Rails 'link_to' seldom works (Unless, of course, you
only stay at the top of the page and do nothing. Rather useless, huh?)

Sorry for the frustration.

Love Rails otherwise.

All better solutions and explanations welcomed. Can someone clear this
up for me? Maybe I am not seeing things the "Rails" way? "RESTful" way?

Many thanks.
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2008-12-19 17:21
> Browser button goes back to correct point in index, you see page exactly
> where you were: top, bottom or WHEREVER IN BETWEEN.

The browser's back button makes no request to the server it simply pulls
the page back from it's local cache. A link on a page regardless of what
it's called makes a request.

>
> 'Redirect_to :back' Rails helper won't work with <%= link_to 'Back', ...
> %>

Making a request causes a page to refresh (unless it's an AJAX request).

This is just how the web works, it has nothing to do with Rails in
particular.

> All better solutions and explanations welcomed. Can someone clear this
> up for me? Maybe I am not seeing things the "Rails" way? "RESTful" way?

Pagination and AJAX have been the best solutions for this particular
issue so far. The browser back button is the bane of web application
development. In more ways than just this particular issue. This is
exactly why AJAX is becoming so popular. It's not really that AJAX is
such a fantastic solution, it's just the best workaround we have for a
system that was not designed for running applications.
This topic is locked and can not be replied to.