Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rails中的JQuery在从另一个页面链接后失败,可用于页面加载_Jquery_Ruby On Rails_Ruby On Rails 4_Zurb Foundation_Turbolinks - Fatal编程技术网

Rails中的JQuery在从另一个页面链接后失败,可用于页面加载

Rails中的JQuery在从另一个页面链接后失败,可用于页面加载,jquery,ruby-on-rails,ruby-on-rails-4,zurb-foundation,turbolinks,Jquery,Ruby On Rails,Ruby On Rails 4,Zurb Foundation,Turbolinks,我有一个报价旋转我在我的主页上使用。当我直接从浏览器加载页面时(在浏览器中键入地址并按enter键),它工作正常。。然而,如果我点击一个链接到我网站的另一个页面,然后链接回主页,它就会停止工作。更具体地说,引号开始重叠,就好像该方法的两个实例正在运行一样 我认为javascript的加载方式可能有问题。因为我在网站的另一个页面上有一个选项卡脚本,它可以很好地加载,但是如果我链接到另一个页面,它就不再工作了 在控制台中未收到任何错误 我在Ubuntu 12上运行Rails 4,Ruby 2.0.0

我有一个报价旋转我在我的主页上使用。当我直接从浏览器加载页面时(在浏览器中键入地址并按enter键),它工作正常。。然而,如果我点击一个链接到我网站的另一个页面,然后链接回主页,它就会停止工作。更具体地说,引号开始重叠,就好像该方法的两个实例正在运行一样

我认为javascript的加载方式可能有问题。因为我在网站的另一个页面上有一个选项卡脚本,它可以很好地加载,但是如果我链接到另一个页面,它就不再工作了

在控制台中未收到任何错误

我在Ubuntu 12上运行Rails 4,Ruby 2.0.0,基金会,并使用WebBLASE进行测试。档案如下:

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0'

# Use mysql as the database for Active Record
gem 'mysql2'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'


# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
#gem 'nokogiri' '~> 1.5.10'
# Use jquery as the JavaScript library
gem 'jquery-rails'

gem 'activerecord-session_store', github: 'rails/activerecord-session_store'

gem 'activemerchant'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

gem 'ransack'

gem 'xml-simple'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

gem 'zurb-foundation'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

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

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

# Use debugger
# gem 'debugger', group: [:development, :test]
脚本:

    function rotateQuotes() {
            var oCurQuote = $('#quotes div.current');
            var oNxtQuote = oCurQuote.next();
            if (oNxtQuote.length == 0)
                oNxtQuote = $('#quotes div:first');

            oCurQuote.removeClass('current').addClass('previous');
            oNxtQuote.css({ opacity: 0.0 }).addClass('current').animate({ opacity: 1.0 }, {duration: 4500},
                function() {
                    oCurQuote.removeClass('previous');});
                    oCurQuote.animate({opacity: 0.0}, {duration: 500});

    }; 

$(function(){

                setInterval(rotateQuotes, 5000);
            });
application.js

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

我猜这与涡轮链接有关。从:

事件

使用Turbolinks,页面将在不完全重新加载的情况下更改,因此您不能依靠
DOMContentLoaded
jQuery.ready()
来触发代码。相反,TurboLink在
文档上触发事件,以提供页面生命周期的挂钩

另外,Rails4默认情况下启用TurboLink(并且您的
应用程序.js中有它),因此
$(function(){…})
在更改页面时不会总是触发。您可以尝试绑定到
turbolinks:load

$(document).on('turbolinks:load', function() {
    setInterval(rotateQuotes, 5000);
});
您可能还需要在访问之前绑定到
turbolinks:before
,以便进行清理


或者,如果您不在乎,您可以禁用TurboLink。

我猜这与TurboLink有关。从:

事件

使用Turbolinks,页面将在不完全重新加载的情况下更改,因此您不能依靠
DOMContentLoaded
jQuery.ready()
来触发代码。相反,TurboLink在
文档上触发事件,以提供页面生命周期的挂钩

另外,Rails4默认情况下启用TurboLink(并且您的
应用程序.js中有它),因此
$(function(){…})
在更改页面时不会总是触发。您可以尝试绑定到
turbolinks:load

$(document).on('turbolinks:load', function() {
    setInterval(rotateQuotes, 5000);
});
您可能还需要在访问之前绑定到
turbolinks:before
,以便进行清理


或者,如果你不在乎的话,你可以禁用TurboLink。

哇,非常感谢!我已经为此挣扎了一天。默认情况下禁用Turbolinks之类的东西对我来说似乎有点粗鲁,我相信很多人都有类似的问题。哇,非常感谢!我已经为此挣扎了一天。默认情况下禁用Turbolinks之类的东西对我来说似乎有点粗鲁,我相信很多人都有类似的问题。