如何从 rails 完全禁用 turbo 链接
how to disable turbo links completely from rails
我浏览了很多帖子和 turbolink 的文档,但我想我还没有完全理解。我试图在 rails 5 应用程序中完全关闭 turbolinks。这是我的处理方式
- 已删除
gem 'trubolinks'
- 从 application.js
中删除了所有出现的 //=require turbolinks
- 从
stylesheet_link_tag
和 javascript_include_tag
中删除了 data-turbolinks-track
application.html.erb 现在看起来像这样
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= content_for?(:title) ? yield(:title) : "Website" %></title>
<meta name="description" content="<%= content_for?(:description) ? yield(:description) : "Website" %>">
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= stylesheet_link_tag params[:controller] %>
<%= javascript_include_tag 'application' %>
<%= javascript_include_tag :modernizr %>
<%= javascript_include_tag params[:controller] %>
<%= if(params[:action].eql?'contact')
javascript_include_tag 'https://www.google.com/recaptcha/api.js?onload=vcRecaptchaApiLoaded&render=explicit' , async: 'async', defer: 'defer'
end
%>
<%= csrf_meta_tags %>
</head>
<body data-turbolinks='false'>
...
</body>
</html>
application.js 文件:
//= require jquery
//= require jquery_ujs
//= require lodash
//= require bootstrap-sprockets
//= require jquery.sticky
和application.css
@import "bootstrap-sprockets";
@import "bootstrap";
@import "font-awesome-sprockets";
@import "font-awesome";
@import "normalize-rails";
@import "animate";
....
/* other css stuff */
我在考虑在 body 标签上使用 data-turbolinks=false
应该有助于为该布局中的所有页面禁用它。但这并没有帮助,页面加载失败并出现错误
Sprockets::FileNotFound at /
couldn't find file 'turbolinks' with type 'application/javascript'
Checked in these paths:
/Users/anadi/Code/website/app/assets/config
/Users/anadi/Code/website/app/assets/fonts
/Users/anadi/Code/website/app/assets/images
/Users/anadi/Code/website/app/assets/javascripts
在主页的这一行
<%= image_tag("pages/home/xyz_logo.png", class: "img-responsive", alt: "Customer") %>
如果我查看堆栈跟踪,ActionView 总是依赖于 turbolinks:
sprockets (3.7.0) lib/sprockets/directive_processor.rb:182:in `rescue in block in process_directives'
sprockets (3.7.0) lib/sprockets/directive_processor.rb:179:in `block in process_directives'
sprockets (3.7.0) lib/sprockets/directive_processor.rb:178:in `process_directives'
sprockets (3.7.0) lib/sprockets/directive_processor.rb:83:in `_call'
sprockets (3.7.0) lib/sprockets/directive_processor.rb:68:in `call'
sprockets (3.7.0) lib/sprockets/processor_utils.rb:75:in `call_processor'
sprockets (3.7.0) lib/sprockets/processor_utils.rb:57:in `block in call_processors'
sprockets (3.7.0) lib/sprockets/processor_utils.rb:56:in `call_processors'
sprockets (3.7.0) lib/sprockets/loader.rb:134:in `load_from_unloaded'
sprockets (3.7.0) lib/sprockets/loader.rb:60:in `block in load'
sprockets (3.7.0) lib/sprockets/loader.rb:317:in `fetch_asset_from_dependency_cache'
sprockets (3.7.0) lib/sprockets/loader.rb:44:in `load'
sprockets (3.7.0) lib/sprockets/cached_environment.rb:20:in `block in initialize'
sprockets (3.7.0) lib/sprockets/cached_environment.rb:47:in `load'
sprockets (3.7.0) lib/sprockets/bundle.rb:23:in `block in call'
sprockets (3.7.0) lib/sprockets/utils.rb:196:in `dfs'
sprockets (3.7.0) lib/sprockets/bundle.rb:24:in `call'
sprockets (3.7.0) lib/sprockets/processor_utils.rb:75:in `call_processor'
sprockets (3.7.0) lib/sprockets/processor_utils.rb:57:in `block in call_processors'
sprockets (3.7.0) lib/sprockets/processor_utils.rb:56:in `call_processors'
sprockets (3.7.0) lib/sprockets/loader.rb:134:in `load_from_unloaded'
sprockets (3.7.0) lib/sprockets/loader.rb:60:in `block in load'
sprockets (3.7.0) lib/sprockets/loader.rb:317:in `fetch_asset_from_dependency_cache'
sprockets (3.7.0) lib/sprockets/loader.rb:44:in `load'
sprockets (3.7.0) lib/sprockets/cached_environment.rb:20:in `block in initialize'
sprockets (3.7.0) lib/sprockets/cached_environment.rb:47:in `load'
sprockets (3.7.0) lib/sprockets/base.rb:66:in `find_asset'
sprockets (3.7.0) lib/sprockets/base.rb:73:in `find_all_linked_assets'
sprockets (3.7.0) lib/sprockets/manifest.rb:134:in `block in find'
sprockets (3.7.0) lib/sprockets/manifest.rb:133:in `find'
sprockets-rails (3.1.1) lib/sprockets/railtie.rb:50:in `precompiled_assets'
sprockets-rails (3.1.1) lib/sprockets/railtie.rb:35:in `asset_precompiled?'
sprockets-rails (3.1.1) lib/sprockets/railtie.rb:250:in `block (3 levels) in <class:Railtie>'
sprockets-rails (3.1.1) lib/sprockets/rails/helper.rb:342:in `precompiled?'
sprockets-rails (3.1.1) lib/sprockets/rails/helper.rb:346:in `raise_unless_precompiled_asset'
sprockets-rails (3.1.1) lib/sprockets/rails/helper.rb:313:in `asset_path'
sprockets-rails (3.1.1) lib/sprockets/rails/helper.rb:85:in `block in resolve_asset_path'
sprockets-rails (3.1.1) lib/sprockets/rails/helper.rb:225:in `block in resolve_asset'
sprockets-rails (3.1.1) lib/sprockets/rails/helper.rb:224:in `resolve_asset'
sprockets-rails (3.1.1) lib/sprockets/rails/helper.rb:84:in `resolve_asset_path'
sprockets-rails (3.1.1) lib/sprockets/rails/helper.rb:74:in `compute_asset_path'
actionview (5.0.0) lib/action_view/helpers/asset_url_helper.rb:144:in `asset_path'
actionview (5.0.0) lib/action_view/helpers/asset_url_helper.rb:311:in `image_path'
actionview (5.0.0) lib/action_view/helpers/asset_tag_helper.rb:214:in `image_tag'
看起来 sprockets 仍然需要 turbolinks js 或者 image_tag url 需要它。我不想最终在每个 link 或图像上添加 data: { turbolinks: false }
。如何让它工作?
- 从您的 Gemfile 中删除 gem 'turbolinks' 行。
- 从您的 app/assets/javascripts/application.js.
中删除 //= 需要 turbolinks
- 删除两个 "data-turbolinks-track" => 真正的散列 key/value 对
来自你的 app/views/layouts/application.html.erb.
也许我做错了,重新启用所有这些,还添加了 data-turbolinks-track => true
,并保留了 <body data-turbolinks='false'>
。它现在按预期工作,没有抛出错误。
我浏览了很多帖子和 turbolink 的文档,但我想我还没有完全理解。我试图在 rails 5 应用程序中完全关闭 turbolinks。这是我的处理方式
- 已删除
gem 'trubolinks'
- 从 application.js 中删除了所有出现的
- 从
stylesheet_link_tag
和javascript_include_tag
中删除了
//=require turbolinks
data-turbolinks-track
application.html.erb 现在看起来像这样
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= content_for?(:title) ? yield(:title) : "Website" %></title>
<meta name="description" content="<%= content_for?(:description) ? yield(:description) : "Website" %>">
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= stylesheet_link_tag params[:controller] %>
<%= javascript_include_tag 'application' %>
<%= javascript_include_tag :modernizr %>
<%= javascript_include_tag params[:controller] %>
<%= if(params[:action].eql?'contact')
javascript_include_tag 'https://www.google.com/recaptcha/api.js?onload=vcRecaptchaApiLoaded&render=explicit' , async: 'async', defer: 'defer'
end
%>
<%= csrf_meta_tags %>
</head>
<body data-turbolinks='false'>
...
</body>
</html>
application.js 文件:
//= require jquery
//= require jquery_ujs
//= require lodash
//= require bootstrap-sprockets
//= require jquery.sticky
和application.css
@import "bootstrap-sprockets";
@import "bootstrap";
@import "font-awesome-sprockets";
@import "font-awesome";
@import "normalize-rails";
@import "animate";
....
/* other css stuff */
我在考虑在 body 标签上使用 data-turbolinks=false
应该有助于为该布局中的所有页面禁用它。但这并没有帮助,页面加载失败并出现错误
Sprockets::FileNotFound at /
couldn't find file 'turbolinks' with type 'application/javascript'
Checked in these paths:
/Users/anadi/Code/website/app/assets/config
/Users/anadi/Code/website/app/assets/fonts
/Users/anadi/Code/website/app/assets/images
/Users/anadi/Code/website/app/assets/javascripts
在主页的这一行
<%= image_tag("pages/home/xyz_logo.png", class: "img-responsive", alt: "Customer") %>
如果我查看堆栈跟踪,ActionView 总是依赖于 turbolinks:
sprockets (3.7.0) lib/sprockets/directive_processor.rb:182:in `rescue in block in process_directives'
sprockets (3.7.0) lib/sprockets/directive_processor.rb:179:in `block in process_directives'
sprockets (3.7.0) lib/sprockets/directive_processor.rb:178:in `process_directives'
sprockets (3.7.0) lib/sprockets/directive_processor.rb:83:in `_call'
sprockets (3.7.0) lib/sprockets/directive_processor.rb:68:in `call'
sprockets (3.7.0) lib/sprockets/processor_utils.rb:75:in `call_processor'
sprockets (3.7.0) lib/sprockets/processor_utils.rb:57:in `block in call_processors'
sprockets (3.7.0) lib/sprockets/processor_utils.rb:56:in `call_processors'
sprockets (3.7.0) lib/sprockets/loader.rb:134:in `load_from_unloaded'
sprockets (3.7.0) lib/sprockets/loader.rb:60:in `block in load'
sprockets (3.7.0) lib/sprockets/loader.rb:317:in `fetch_asset_from_dependency_cache'
sprockets (3.7.0) lib/sprockets/loader.rb:44:in `load'
sprockets (3.7.0) lib/sprockets/cached_environment.rb:20:in `block in initialize'
sprockets (3.7.0) lib/sprockets/cached_environment.rb:47:in `load'
sprockets (3.7.0) lib/sprockets/bundle.rb:23:in `block in call'
sprockets (3.7.0) lib/sprockets/utils.rb:196:in `dfs'
sprockets (3.7.0) lib/sprockets/bundle.rb:24:in `call'
sprockets (3.7.0) lib/sprockets/processor_utils.rb:75:in `call_processor'
sprockets (3.7.0) lib/sprockets/processor_utils.rb:57:in `block in call_processors'
sprockets (3.7.0) lib/sprockets/processor_utils.rb:56:in `call_processors'
sprockets (3.7.0) lib/sprockets/loader.rb:134:in `load_from_unloaded'
sprockets (3.7.0) lib/sprockets/loader.rb:60:in `block in load'
sprockets (3.7.0) lib/sprockets/loader.rb:317:in `fetch_asset_from_dependency_cache'
sprockets (3.7.0) lib/sprockets/loader.rb:44:in `load'
sprockets (3.7.0) lib/sprockets/cached_environment.rb:20:in `block in initialize'
sprockets (3.7.0) lib/sprockets/cached_environment.rb:47:in `load'
sprockets (3.7.0) lib/sprockets/base.rb:66:in `find_asset'
sprockets (3.7.0) lib/sprockets/base.rb:73:in `find_all_linked_assets'
sprockets (3.7.0) lib/sprockets/manifest.rb:134:in `block in find'
sprockets (3.7.0) lib/sprockets/manifest.rb:133:in `find'
sprockets-rails (3.1.1) lib/sprockets/railtie.rb:50:in `precompiled_assets'
sprockets-rails (3.1.1) lib/sprockets/railtie.rb:35:in `asset_precompiled?'
sprockets-rails (3.1.1) lib/sprockets/railtie.rb:250:in `block (3 levels) in <class:Railtie>'
sprockets-rails (3.1.1) lib/sprockets/rails/helper.rb:342:in `precompiled?'
sprockets-rails (3.1.1) lib/sprockets/rails/helper.rb:346:in `raise_unless_precompiled_asset'
sprockets-rails (3.1.1) lib/sprockets/rails/helper.rb:313:in `asset_path'
sprockets-rails (3.1.1) lib/sprockets/rails/helper.rb:85:in `block in resolve_asset_path'
sprockets-rails (3.1.1) lib/sprockets/rails/helper.rb:225:in `block in resolve_asset'
sprockets-rails (3.1.1) lib/sprockets/rails/helper.rb:224:in `resolve_asset'
sprockets-rails (3.1.1) lib/sprockets/rails/helper.rb:84:in `resolve_asset_path'
sprockets-rails (3.1.1) lib/sprockets/rails/helper.rb:74:in `compute_asset_path'
actionview (5.0.0) lib/action_view/helpers/asset_url_helper.rb:144:in `asset_path'
actionview (5.0.0) lib/action_view/helpers/asset_url_helper.rb:311:in `image_path'
actionview (5.0.0) lib/action_view/helpers/asset_tag_helper.rb:214:in `image_tag'
看起来 sprockets 仍然需要 turbolinks js 或者 image_tag url 需要它。我不想最终在每个 link 或图像上添加 data: { turbolinks: false }
。如何让它工作?
- 从您的 Gemfile 中删除 gem 'turbolinks' 行。
- 从您的 app/assets/javascripts/application.js. 中删除 //= 需要 turbolinks
- 删除两个 "data-turbolinks-track" => 真正的散列 key/value 对 来自你的 app/views/layouts/application.html.erb.
也许我做错了,重新启用所有这些,还添加了 data-turbolinks-track => true
,并保留了 <body data-turbolinks='false'>
。它现在按预期工作,没有抛出错误。