I'm trying to make a JS snippet work in a concrete page with Rails 4 and Turbolinks. I've tried with the standard solution:
我试图让一个JS片段在一个带有Rails 4和Turbolinks的具体页面中工作。我尝试过标准的解决方案:
<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 %>
8 个解决方案
#1
47
Add “data-no-turbolink” to the <body> tag
of the the page you want it disabled on
将“data-no-turbolink”添加到要禁用的页面的标记
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],然后将它添加到一个区域
#2
52
Here's a cleaner solution:
这里有一个更清洁的解决方案:
In /app/views/layouts/application.html.erb
, replace the <body>
tag with this:
在/ app / views /布局/ application.html。erb,将标签替换为:
<body
<% if content_for?(:body_attributes) %>
<%= yield(:body_attributes) %>
<% end %>>
Now, if you want to disable turbolinks in a particular view, e.g. /app/views/home/index.html.erb
, you can add this to the file:
现在,如果您想禁用特定视图中的turbolinks,例如/app/view /home/index.html。erb,你可以把这个添加到文件中:
for Rails 4
<% content_for(:body_attributes) do %>
data-no-turbolink="true"
<% end %>
and that will end up rendering as:
最终呈现为:
<body data-no-turbolink="true">
for Rails 5
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">
#3
12
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改变了新版本中禁用单个页面上的Turbolinks的语法(5.0.0)。
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+的页面上禁用它,请将数据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:
它也适用于任何链接的祖先,所以在这个例子中,这两个链接将导致非turbolinked页面:
<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
来源:Turbolinks在GitHub上
#4
9
Just a slightly modified version of fearless_fool's answer, which rendered strangely due to white space and quotes:
这只是一个稍作修改的可怕傻瓜的答案,由于空格和引号,这个答案显得很奇怪:
Application.html.erb
Application.html.erb
<body <%= yield(:body_attributes) %>>
View.html.erb
View.html.erb
<%= content_for(:body_attributes, 'data-no-turbolink') %>
#5
3
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>
#6
1
For anyone is already using rails 5. If you want to disable entire turbolink for a specific page, just add this line "data-turbolinks='false'" to the body of that page:
任何人都已经在使用rails 5了。如果您想要禁用特定页面的整个turbolink,只需在页面主体中添加这一行“data-turbolinks='false'”:
<body data-turbolinks="false">
#7
0
Here is a solution that works by disabling turbolinks on the page that a link links to. In this case, the 'edit post' page:
这里有一个解决方案,可以通过禁用链接链接到的页面上的turbolinks来实现。在本例中,“编辑后”页:
<%= link_to 'Edit', edit_post_path(@post), 'data-no-turbolink' => true %>
#8
0
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'
#1
47
Add “data-no-turbolink” to the <body> tag
of the the page you want it disabled on
将“data-no-turbolink”添加到要禁用的页面的标记
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],然后将它添加到一个区域
#2
52
Here's a cleaner solution:
这里有一个更清洁的解决方案:
In /app/views/layouts/application.html.erb
, replace the <body>
tag with this:
在/ app / views /布局/ application.html。erb,将标签替换为:
<body
<% if content_for?(:body_attributes) %>
<%= yield(:body_attributes) %>
<% end %>>
Now, if you want to disable turbolinks in a particular view, e.g. /app/views/home/index.html.erb
, you can add this to the file:
现在,如果您想禁用特定视图中的turbolinks,例如/app/view /home/index.html。erb,你可以把这个添加到文件中:
for Rails 4
<% content_for(:body_attributes) do %>
data-no-turbolink="true"
<% end %>
and that will end up rendering as:
最终呈现为:
<body data-no-turbolink="true">
for Rails 5
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">
#3
12
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改变了新版本中禁用单个页面上的Turbolinks的语法(5.0.0)。
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+的页面上禁用它,请将数据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:
它也适用于任何链接的祖先,所以在这个例子中,这两个链接将导致非turbolinked页面:
<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
来源:Turbolinks在GitHub上
#4
9
Just a slightly modified version of fearless_fool's answer, which rendered strangely due to white space and quotes:
这只是一个稍作修改的可怕傻瓜的答案,由于空格和引号,这个答案显得很奇怪:
Application.html.erb
Application.html.erb
<body <%= yield(:body_attributes) %>>
View.html.erb
View.html.erb
<%= content_for(:body_attributes, 'data-no-turbolink') %>
#5
3
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>
#6
1
For anyone is already using rails 5. If you want to disable entire turbolink for a specific page, just add this line "data-turbolinks='false'" to the body of that page:
任何人都已经在使用rails 5了。如果您想要禁用特定页面的整个turbolink,只需在页面主体中添加这一行“data-turbolinks='false'”:
<body data-turbolinks="false">
#7
0
Here is a solution that works by disabling turbolinks on the page that a link links to. In this case, the 'edit post' page:
这里有一个解决方案,可以通过禁用链接链接到的页面上的turbolinks来实现。在本例中,“编辑后”页:
<%= link_to 'Edit', edit_post_path(@post), 'data-no-turbolink' => true %>
#8
0
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'