45

Just wondering whether there's a way to use turbolinks directly in a rails link_to helper, a quick bit of googling didn't uncover anything of note, here's the type of thing I've tried to no avail.

<%= link_to 'Giraffe', giraffe_path(@giraffe), :data-no-turbolink => 'true' %>
<%= link_to 'Giraffe', giraffe_path(@giraffe), :data { :no-turbolink => 'true'} %>

I know you can do it in regular links like this

<a data-no-turbolink='true' href="/giraffe-130">Giraffe</a>

Right now I'm just including the attribute on elements that surround the link such as lis or divs.

Thanks in advance.

Gareth Jones
  • 1,760
  • 1
  • 14
  • 24

7 Answers7

107

Edit for Rails 5+: @ManishShrivastava correctly pointed out the different syntax needed for Rails 5 as shown in Joseph's answer.

<%= link_to('Giraffe', @giraffe, data: { turbolinks: false }) %>

For Rails 4 and below

Originally I thought you needed to use the hash rocket syntax for the symbol but that isn't the case. You can use a data: hash and inside that hash any symbols using underscores _ will be converted to dashes -.

I think most Rails developers would prefer to see the following (including myself now that I know better):

<%= link_to('Giraffe', @giraffe, data: { no_turbolink: true }) %>

But the following also works:

<%= link_to('Giraffe', @giraffe, 'data-no-turbolink' => true) %>

Gerry Shaw
  • 9,178
  • 5
  • 41
  • 45
  • I feel bad about upvoting an answer with 42 votes, even if I really want to. Otherwise, thanks I didn't know about this underscore-dash conversion ! Then it means it's not possible to have the actual underscore in the output ? – Cyril Duchon-Doris Mar 07 '15 at 19:38
  • If you want an underscore in the output you would have to use quotes and the hash rocket syntax. – Gerry Shaw May 10 '15 at 15:40
  • 1
    @ManishShrivastava thanks for pointing out the edit needed for Rails 5. – Gerry Shaw Dec 14 '18 at 17:58
71

Turbolinks 5 uses a slightly different syntax

<%= link_to "Foo", new_foo_path(@foo), data: { turbolinks: false } %>

Source: Turbolinks Github Page

Joseph N.
  • 2,437
  • 1
  • 25
  • 31
  • 2
    After upgrading to Rails 5, this is definitively something to look out for. Took me a while to realize that the attribute naming had changed. – bovender Sep 10 '16 at 06:23
  • This is for rails 5. This only gonna work. https://github.com/turbolinks/turbolinks/issues/269#issuecomment-297541803 – Manish Shrivastava Dec 13 '18 at 12:02
17

You can use a symbol without problems to generate the following code:

 <a data-no-turbolink='true' href="/giraffe-130">Giraffe</a>

Just do the following:

 <%= link_to 'Giraffe', giraffe_path(@giraffe),
             :data => { :no_turbolink => true } %>

Note: :no_turbolink will become no-turbolink and the value will be converted to JSON automatically using to_json, e.g. true to "true".

Daniel Rikowski
  • 71,375
  • 57
  • 251
  • 329
Dinuz
  • 400
  • 4
  • 13
  • I think this should be the correct answer. It fixes typos in the original question without giving him a different way. – Jeff Adams Sep 25 '13 at 17:13
6

@Joseph you are right, but only small change:

<%= link_to "Foo", new_foo_path(@foo), data: { turbolinks: 'false' } %>

when I'm typing turbolinks: false doesn't work with bool, works only when I use string for value turbolinks: 'false'. So maybe someone help this information ;).

Štefan Bartoš
  • 561
  • 6
  • 8
4

Or Ruby 1.9+ syntax:

<%= link_to 'Foo', foo_path(@foo), data: { no_turbolink: true } %>

But I ended up dropping turbolinks in favour of Wiselinks which makes behaviour explicit on all links, plus Wiselinks also supports partial update (eg paging), replace state (doesn't pollute browser history, great for column sorting), form submission (great for search forms), redirects, support for browsers without history API, and more intelligent asset change handling.

Andrew Hacking
  • 6,296
  • 31
  • 37
0

when you'd like to change language using locale, you have do like here:

<%= link_to content_tag(:span, "Українською"),  {locale: :uk},  
data:{ turbolinks: "false" }%>
andriy
  • 111
  • 3
0

Rails 7 is now

<%= link_to 'Foo', foo_path(@foo), data: { turbo: false } %>
Mark Swardstrom
  • 17,217
  • 6
  • 62
  • 70