Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using turbolinks in a Rails link_to

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.

like image 222
Gareth Jones Avatar asked Jan 09 '13 02:01

Gareth Jones


People also ask

What are Turbolinks in Rails?

Turbolinks is a Rails feature, available as a gem and enabled by default in new Rails apps. It is intended to speed up navigating between pages of your application.

What does Turbolinks do with your browser's history?

Turbolinks saves a copy of the current page to its cache just before rendering a new page. Note that Turbolinks copies the page using cloneNode(true) , which means any attached event listeners and associated data are discarded.

How do I disable Turbolinks?

If you want to disable Turbolinks for certain links, add a data-turbolinks="false" attribute to the tag: < a href = "..." data-turbolinks = "false" >No turbolinks here</ a >.


1 Answers

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) %>

like image 181
Gerry Shaw Avatar answered Oct 04 '22 18:10

Gerry Shaw