[英]Rails 4: disable Turbolinks in a specific page
I'm trying to make a JS snippet work in a concrete page with Rails 4 and Turbolinks.我正在尝试使用 Rails 4 和 Turbolinks 在具体页面中制作 JS 代码段。 I've tried with the standard solution:我已经尝试过标准解决方案:
<script type="text/javascript">
var ready = function() {
// Bla bla
};
$(document).ready(ready);
$(document).on('page:load', ready);
</script>
But it doesn't seem to work.但它似乎不起作用。 My snippet is something like this:我的片段是这样的:
<script type="text/javascript">
function ismready()
{
var API_KEY = "api key";
var roomId = "room id";
var ism = new Ismuser(API_KEY, roomId);
ism.init({
board: {
layer: "board"
},
video: {
camsContainer: "guest"
},
chat: {
layer: "chat"
},
moderator: true,
});
}
</script>
<script src="http://assets.ismuser.com/v0.4/js/ismuser.js" type="text/javascript"></script>
The snippet doesn't work as expected (even with the standard solution) and finally I just want to disable Turbolinks in this page.该代码段没有按预期工作(即使使用标准解决方案),最后我只想在此页面中禁用 Turbolinks。
How can i do it?我该怎么做?
-- Solution - 解决方案
<% content_for :body do %>
<% if controller.controller_name == 'home' && controller.action_name == 'demo1' %>
<body data-no-turbolink="true">
<% end %>
<% end %>
Here's a cleaner solution:这是一个更清洁的解决方案:
In /app/views/layouts/application.html.erb
, replace the <body>
tag with this:在/app/views/layouts/application.html.erb
,将<body>
标签替换为:
<body
<% if content_for?(:body_attributes) %>
<%= yield(:body_attributes) %>
<% end %>>
Now, if you want to disable turbolinks in a particular view, eg /app/views/home/index.html.erb
, you can add this to the file:现在,如果您想在特定视图中禁用 turbolinks,例如/app/views/home/index.html.erb
,您可以将其添加到文件中:
<% content_for(:body_attributes) do %>
data-no-turbolink="true"
<% end %>
and that will end up rendering as:这将最终呈现为:
<body data-no-turbolink="true">
In Rails 5, the syntax is slightly different:在 Rails 5 中,语法略有不同:
<% content_for(:body_attributes) do %>
data-turbolinks="false"
<% end %>
and that will end up rendering as:这将最终呈现为:
<body data-turbolinks="false">
Add “data-no-turbolink” to the <body> tag
of the the page you want it disabled on将“data-no-turbolink” to the <body> tag
添加“data-no-turbolink” to the <body> tag
您希望禁用它的页面“data-no-turbolink” to the <body> tag
中
If you have a shared layout file which i am assuming you do, you can do an if statement and check the params[:controller] and params[:action] and just add it to the one area如果你有一个我假设你有的共享布局文件,你可以做一个 if 语句并检查 params[:controller] 和 params[:action] 并将它添加到一个区域
Edited Jan 2021: As pointed out by @weltschmerz under rails 5 data-turbolinks="false"
is the preference. 2021 年 1 月编辑:正如@weltschmerz 在 rails 5 data-turbolinks="false"
下指出的那样是首选。
Solutions on here didn't work for me, turns out Turbolinks changed the syntax for disabling Turbolinks on a single page in their new release (5.0.0).此处的解决方案对我不起作用,结果 Turbolinks 在其新版本 (5.0.0) 中更改了在单个页面上禁用 Turbolinks 的语法。
To disable it on a page with Turbolinks 5.0.0+, add data-turbolinks="false"
to the links of the page you want to disable:要在使用 Turbolinks 5.0.0+ 的页面上禁用它,请将data-turbolinks="false"
到要禁用的页面的链接:
<a href="/link" data-turbolinks="false">Page without Turbolinks</a>
It also works on any of the links' ancestors, so in this example both links will lead to non-turbolinked pages:它也适用于任何链接的祖先,所以在这个例子中,两个链接都将导致非涡轮链接页面:
<div data-turbolinks="false">
<a href="/link1">Page without Turbolinks</a>
<a href="/link2">Another page without Turbolinks</a>
</div>
To enable it on a single link with all the other links disabled in a specific element:要在单个链接上启用它,而在特定元素中禁用所有其他链接:
<div data-turbolinks="false">
<a href="/link1">Page without Turbolinks</a>
<a href="/link2">Another page without Turbolinks</a>
<a href="/link3" data-turbolinks="true">Page with Turbolinks enabled</a>
</div>
I also tried adding it to the body of the page I want it disabled on, similar to the old method but with using data-turbolinks="false"
instead of data-no-turbolink="true"
- and that worked too!我还尝试将它添加到我希望禁用它的页面的正文中,类似于旧方法,但使用data-turbolinks="false"
而不是data-no-turbolink="true"
- 这也有效!
Source: Turbolinks on GitHub来源: GitHub 上的 Turbolinks
Just a slightly modified version of fearless_fool's answer, which rendered strangely due to white space and quotes:只是fearless_fool 答案的一个稍微修改的版本,由于空格和引号而变得奇怪:
Application.html.erb应用程序.html.erb
<body <%= yield(:body_attributes) %>>
View.html.erb查看.html.erb
<%= content_for(:body_attributes, 'data-no-turbolink') %>
Here is an alternative way to code this up, I just choose which tag to display based on the controller name.这是另一种编码方式,我只是根据控制器名称选择要显示的标签。
<html>
<head></head>
<% if controller.controller_name == 'subscriptions' %>
<body data-no-turbolink>
<% else %>
<body >
<% end %>
Add the rest of my body here...
</body>
</html>
Here's a solution in Haml
with Rails 5.2.1
这是Haml
with Rails 5.2.1
的解决方案
In your application.haml
:在您的application.haml
:
%body{data: {turbolinks: (content_for :turbolinks)}}
In your template.haml
:在你的template.haml
:
- content_for :turbolinks {"false"}
对于已经在使用 rails 5 的任何人。如果您想禁用特定页面的整个 turbolink,只需将此行“data-turbolinks='false'”添加到该页面的正文中:
<body data-turbolinks="false">
Here is a solution that works by disabling turbolinks on the page that a link links to.这是一个解决方案,它通过在链接链接到的页面上禁用 turbolinks 来工作。 In this case, the 'edit post' page:在这种情况下,“编辑帖子”页面:
<%= link_to 'Edit', edit_post_path(@post), 'data-no-turbolink' => true %>
This worked.这奏效了。
Where ever the link is to the page do something like this链接到页面的地方做这样的事情
%div{"data-turbolinks" => "false"}
= link_to 'Send payment', new_payments_manager_path(sender_id: current_user.id, receiver_id: @collaboration.with(current_user).id, collaboration_id: params[:id]), class: 'button'
I had a situation in a Rails 5.2.4.4 app where I had a script working before I added turbolinks.我在 Rails 5.2.4.4 应用程序中遇到过一种情况,在添加 turbolinks 之前,我有一个脚本可以工作。 All I did was include turbolinks in application.js.我所做的只是在 application.js 中包含 turbolinks。 Doing that broke my working script.这样做破坏了我的工作脚本。 I did several things in application.html.erb to get turbolinks working on a single page.我在 application.html.erb 中做了几件事来让 turbolinks 在单个页面上工作。
I used the solution of @Coherent for the body statement.我将@Coherent 的解决方案用于正文语句。 I also retrieve the action and controller values to use in my turbolinks script.我还检索了要在我的 turbolinks 脚本中使用的操作和控制器值。
<% if params[:action] == "action_name" && params[:controller] == "controller_name" %>
<body data-action='<%= "#{params[:action]}" %>' data-controller='<%= "#{params[:controller]}" %>'>
<% else %>
<body data-turbolinks="false" data-action='<%= "#{params[:action]}" %>' data-controller='<%= "#{params[:controller]}" %>'>
<% end %>
This is how I wrote my turbolinks script in the head section.这就是我在 head 部分编写 turbolinks 脚本的方式。 When I look at the source the turbolinks script is included on the selected page.当我查看源代码时,turbolinks 脚本包含在所选页面中。 Otherwise the comment appears on the page.否则评论会出现在页面上。
<% if params[:action] == "action_name" && params[:controller] == "controller_name" %>
<script>
my turbolinks script logic here
</script>
<% else %>
<!-- some logic here -->
<% end %>
I tried disabling each link by adding data-turbolinks="false"
on the page where my original script was working.我尝试通过在原始脚本工作的页面上添加data-turbolinks="false"
来禁用每个链接。 I could not get it to work.我无法让它工作。 I added the following statement to the head section.我在 head 部分添加了以下语句。 I found this here in the Turbolinks documentation.我发现这个在这里的Turbolinks文档。 If all else fails this statement seems to work.如果所有其他方法都失败了,则此声明似乎有效。
<meta name="turbolinks-visit-control" content="reload">
Both scripts work as expected.两个脚本都按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.