Turbolinks无法正常工作(页面加载两次)

时间:2017-01-08 17:33:22

标签: javascript jquery ruby-on-rails

我的网页被加载了两次(第一次非常快btw)。除此之外,我必须刷新页面以让我的j工作
application.html.erb

<head>
  <title><%= content_for?(:title) ? yield(:title) : "ASC Engenharia e Construções" %> </title>

  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
  <meta name="viewport" content="width=device-width, initial-scale=1">

</head>


的application.js

//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require bootstrap-sprockets
//= require dataTables/jquery.dataTables
//= require dataTables/bootstrap/3/jquery.dataTables.bootstrap
//= require fancybox
//= require flash.js
//= require acompanhamento.js
//= require controle_de_obras.js.coffee
//= require documentos.js.coffee
//= require funcionarios.coffee
//= require galeria.coffee
//= require turbolinks


我已经执行了rails assets:clean。已经删除了public/asstets目录,并检查过它不是chrome bug。

的Gemfile

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.4'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

gem "font-awesome-rails"

gem 'bootstrap-sass', '~> 3.3.6'

# Use jquery as the JavaScript library
gem 'jquery-rails'

gem 'jquery-ui-rails'
gem 'jquery-datatables-rails', git: 'git://github.com/rweng/jquery-datatables-rails.git'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks', '~> 5.0.0'
gem 'jquery-turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

gem 'devise', '~> 3.4.1'

gem "paperclip", "~> 5.0.0"
gem 'fancybox2-rails', '~> 0.2.8'
gem "rails_admin"
gem "prawn"
gem 'prawn-table', '~> 0.1.0'
gem "rails_admin_import", "~> 2.0"


# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

2 个答案:

答案 0 :(得分:0)

您需要使用//= require_tree .的所有内容,并且最重要的是您需要上述所有内容两次。摆脱最后一行并手动要求所有的js文件。希望这有帮助!

// load jQuery, turbolinks, and ujs
//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs

// load all scripts
//= require dataTables/jquery.dataTables
//= require dataTables/bootstrap/3/jquery.dataTables.bootstrap
//= require bootstrap-sprockets
//= require fancybox

// load turbolinks
//= require turbolinks

// get rid of this line
//= require_tree . <-- this line requires all js files

答案 1 :(得分:0)

根据文档,这两个选项中的任何一个都“保证可以正常工作”,参考文献如下:

$(document).ready(function () { /* ... */ });

$(function () { /* ... */ });

要了解更多文档,请here