Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...