A link back to referring page in Rails

So here’s the quick problem. You have a long list of records in your rails application, such as posts. You end up using the standard pagination offered in rails, which we all know is slow. Regardless, it’s pretty annoying to go down into an individual post, edit it in-place, and then hit the back button, since there’s no link back to the previous page.

Well, since it’s a pagination, there’s not a static page to link a “back to list” link. You could pass in the page, but that’s a pain in the butt. It’s much easier to do:

This’ll put a link back to the same page in the pagination list that you came from. Saves pains. Ends up reading the HTTP spec on headers is a helpful, in addition to some Rails source.

Ends up that “redirect_to :back” also uses the same trick. You can use that in your controller to just redirect back to whatever method called it.


4 thoughts on “A link back to referring page in Rails

  1. I know this is an old post, but I'd like to point out that:

    < %= link_to "Back up to list", :back %>

    will accomplish the same thing, and if there isn't a referrer, it will use the javascript:history.back() method..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s