Using turbolinks in a Rails link_to

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 10:43: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.


回答1:


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




回答2:


Turbolinks 5 uses a slightly different syntax

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

Source: Turbolinks Github Page




回答3:


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".




回答4:


@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 ;).




回答5:


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.




回答6:


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

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


来源:https://stackoverflow.com/questions/14227363/using-turbolinks-in-a-rails-link-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!