link_to helper doesn't work with :back #27

loddy1234 opened this Issue Sep 27, 2012 · 26 comments

<%= link_to "Cancel", :back %> generates a link to the current page


Could be fixed by setting referer on fetchReplacement


I tried this:

fetchReplacement = (url) ->
  xhr = new XMLHttpRequest 'GET', url, true
  xhr.setRequestHeader 'Accept', 'text/html, application/xhtml+xml, application/xml'
  xhr.setRequestHeader 'Referer', historyCache[window.history.state.position - 1].url
  xhr.onload  = -> fullReplacement xhr.responseText, url
  xhr.onabort = -> console.log "Aborted turbolink fetch!"

But it doesn't work (on chrome) and gives the warning Refused to set unsafe header "Referer". I don't think it'll possible as setting the referer is disallowed by the XHR spec (see here).


Ah, so are we going to have to do a custom header var and server side handling to override referrer?


This is actually a history pushstate bug and is already fixed in chromium

May have to wait for that.


perhaps we can add header X-Push-State-Referer to the request, and leave it to the user to implement a server side solution.


Recommend using alias_chain_method for Rack::Request referer method...something like

def referer_with_turbolinks
  @env['X_PUSH_STATE_REFERER'] || referer_without_turbolinks
alias_chain_method :referer,:turbolinks

If we are ok with that approach, ill implement later.


personally I almost never use :back, i prefer to be explicit on the redirects. doing it at the rack level would ensure rails treats the request just like any other regular request.


We do have to override redirect_to, checked source it uses


not request.referer to determine back.

dhh commented Oct 2, 2012

Fixed in 46818ee

@dhh dhh closed this Oct 2, 2012

I think this issue still occurs and I am using turbolinks 1.1.1. Does anyone else have problems with the link_to :back helper generating a link to the current page?

<%= link_to "Back", :back %>

One way to make :back option works, we have to override HTTP_REFERER in request header, however it is blocked by browser.

Another way to make :back option works, we can change _back_url method defined in actionpack/lib/action_view/helpers/url_helper.rb, like this

def _back_url # :nodoc:
  referrer = controller.respond_to?(:request) && (controller.request.env["HTTP_X_XHR_RFERER"] || controller.request.env["HTTP_REFERER"])
  referrer || 'javascript:history.back()'

But this makes the newly potential xss pitfall, since attacker can prepare to send another referrer by setting X-XHR-Referer header.

So in my opinion, to use javascrtip:hisotry.back() you should write it to link_to target path, or hook history.back() on clicking the link.


+1 with @chourobin that link_to back doesn't work on neither v1 nor v1.1.1


+1 @lecky it's funny how we run into the same bug

@kuboon kuboon referenced this issue in heartrails/rails4-example May 14, 2013

back ボタンが効かない #15


+1 . @kuboon, thx!


I'm on turbolinks 1.1.1 and still seeing this occur.


Still seeing this happen in Rails 4 rc2 with turbolinks 1.2.0


PR #234 contains a fix


This issue still exists with Rails 4.0.2, Turbolinks 2.2.0. Here's how to reproduce:

$  rails new TurbolinksTest
$  cd TurbolinksTest
$  rails generate controller First
$  rails generate controller Second
$  vi config/routes.rb
root 'first#index'
get 'second' => 'second#index'

$  vi app/views/first/index.html.erb
<h2>First Page</h2>
<%= link_to 'Second', second_path %>

$  vi app/views/second/index.html.erb
<h2>Second Page</h2>
<%= link_to 'Back', :back %>

$  rails s

Load http://localhost:3000, click on "Second" link, once on the 2nd page click on "Back" link. You stay at the 2nd page.

Now reload the page. The link changes to javascript:history.back() .

Tested in Firefox 25 and Chrome 32.


Just to close the loop on this, @reed's commit above fixed my issue. Thanks!


@reed It is still not working on Safari 7.0.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment