导轨4,Turbolinks 3,部分更换

时间:2022-06-15 15:16:51

How to use this new Turbolinks 3.0 (branch: 'master') with Rails 4.2?

如何在Rails 4.2中使用这个新的Turbolinks 3.0(分支:'master')?

I'm trying and I got from Chrome this error:

我正在尝试,我从Chrome获得此错误:

Uncaught SyntaxError: Unexpected token :

未捕获的SyntaxError:意外的令牌:

If I use partial replacement like this:

如果我使用这样的部分替换:

<%= link_to "Something", root_path %>

and then in the same page:

然后在同一页面中:

<script> Turbolinks.visit(url, { change: 'areaDiv' }); </script>

according to docs: https://github.com/rails/turbolinks#partial-replacements-with-turbolinks-30

根据文档:https://github.com/rails/turbolinks#partial-replacements-with-turbolinks-30

The page is reloaded entirely and I got that error. How to do?

该页面完全重新加载,我收到了该错误。怎么做?

EDIT THE DAY AFTER

编辑后的一天

I have Rails 4.2.1 standard.

我有Rails 4.2.1标准。

In Gemfile I added:

在Gemfile中我添加了:

gem 'turbolinks', :github => 'rails/turbolinks', :branch => 'master'

Now in one controller I have:

现在我在一个控制器中:

def ok_category
      render plain: "Ok", change: 'categoriesdiv'
  end

or maybe this:

或者这个:

 def red_category
      redirect_to "http://www.google.com", change: 'categoriesdiv'
  end

or another like this:

或者像这样:

def red_category
      render partial: 'category_choose', change: 'categoriesdiv'
  end

also if I add the , turbolinks: true at the end of each line, NOTHING!

如果我添加,turbolinks:在每行的末尾都是true,没有!

My chrome window's title is changing in a laconic "undefined", but if I use the Chrome developer tool it says to me in "Network" tab that the request to the server is complete with "GET" method, "304" status, "Text/Javascript" type, all without errors.

我的chrome窗口的标题正在以简洁的“未定义”进行更改,但如果我使用Chrome开发人员工具,它会在“网络”标签中告诉我,对服务器的请求已完成“GET”方法,“304”状态,“文本/ Javascript“类型,所有没有错误。

In the application.html.erb I have:

在application.html.erb中我有:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hello!</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>
<div class="container">

<div id="test"><%= Time.now.to_s %></div>
<div id="links"><%= link_to "Testmylink", ok_category_path(1), remote:true %></div>

<%= yield %>

<div id="categoriesdiv" data-turbolinks-temporary>Yes yes!</div>
<footer class="footer">
  <p class="text-center">Hello!</p>
</footer>
</div>
</body></html>

What is wrong with me?

我是怎么了?

2 个解决方案

#1


Turbolinks is tricky, either way here is a resource worth spending some time on. I see slight syntax differentiation between what you write and how the do.

Turbolinks很棘手,无论哪种方式都值得花一些时间。我看到你写的内容和做法之间存在轻微的语法区别。

https://github.com/rails/turbolinks/issues/448

#2


When using link_to the remote parameter tells rails to use turbolinks to follow the link so I think you need remote: true.

当使用link_to时,远程参数告诉rails使用turbolinks来跟踪链接,所以我认为你需要remote:true。

You also need to add your change area, I'm not certain about that bit but I would go for something like this:

你还需要添加你的更改区域,我不确定那个位,但我会选择这样的东西:

<%= link_to "Something", root_path, remote: true, { :change => 'areaDiv' } %>

#1


Turbolinks is tricky, either way here is a resource worth spending some time on. I see slight syntax differentiation between what you write and how the do.

Turbolinks很棘手,无论哪种方式都值得花一些时间。我看到你写的内容和做法之间存在轻微的语法区别。

https://github.com/rails/turbolinks/issues/448

#2


When using link_to the remote parameter tells rails to use turbolinks to follow the link so I think you need remote: true.

当使用link_to时,远程参数告诉rails使用turbolinks来跟踪链接,所以我认为你需要remote:true。

You also need to add your change area, I'm not certain about that bit but I would go for something like this:

你还需要添加你的更改区域,我不确定那个位,但我会选择这样的东西:

<%= link_to "Something", root_path, remote: true, { :change => 'areaDiv' } %>