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
447 views
in Technique[技术] by (71.8m points)

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

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 %>
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Here's a cleaner solution:

In /app/views/layouts/application.html.erb, replace the <body> tag with this:

<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:

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:

  <% content_for(:body_attributes) do %>
    data-turbolinks="false"
  <% end %>

and that will end up rendering as:

<body data-turbolinks="false">

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

...