在 Rails link_to 中使用 turbolinks [英] Using turbolinks in a Rails link_to
问题描述
只是想知道是否有一种方法可以直接在 rails link_to helper 中使用 turbolinks,快速的谷歌搜索没有发现任何值得注意的东西,这是我尝试过的无济于事的类型.
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>
现在我只是在链接周围的元素(例如 lis 或 divs)上包含属性.
Right now I'm just including the attribute on elements that surround the link such as lis or divs.
提前致谢.
推荐答案
Edit for Rails 5+: @ManishShrivastava 正确指出了 Rails 5 所需的不同语法,如 约瑟夫的回答.
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 }) %>
对于 Rails 4 及以下版本
最初我认为您需要对符号使用哈希火箭语法,但事实并非如此.您可以使用 data:
散列,在该散列中,任何使用下划线 _
的符号都将转换为破折号 -
.
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 -
.
我认为大多数 Rails 开发人员更愿意看到以下内容(包括我现在更了解的自己):
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 }) %>
但以下也有效:
<%= link_to('Giraffe', @giraffe, 'data-no-turbolink' => true) %>
这篇关于在 Rails link_to 中使用 turbolinks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!