为什么jQuery不能在rails上运行?

时间:2013-04-25 03:40:57

标签: jquery ruby-on-rails ruby-on-rails-3

我已完成此命令

bundle exec rake assets:precompile RAILS_ENV=production

现在,加载页面后,加载的jQuery将无法启动:( 它曾经在预编译之前正常工作......

可能的原因是什么?

资产/ Javascript角/ refresh_count.js

    jQuery(document).ready(function () {
        refreshPartialMail();
    });


    function refreshPartialMail() {
      $.ajax({
        url: "/messages/refresh_part",
        type: "GET",
        dataType: "script",
      });
    }

查看

...
<%= javascript_include_tag 'refresh_count' %>
...

生成HTML

<script src="/assets/refresh_count-0cdf67811wb7eavf438be9c8b8cc932bf.js" type="text/javascript"></script>

“/assets/refresh_count-0cdf67811wb7eavf438be9c8b8cc932bf.js”的内容

function refreshPartialMail(){$.ajax({url:"/messages/refresh_part",type:"GET",dataType:"script"})}jQuery(document).ready(function(){refreshPartialMail()});

资产/ Javascript角/ 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
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery-ui
//= require twitter/bootstrap
//= require jquery_ujs
//= require jquery.ui.datepicker
//= require autocomplete-rails

在执行precompile执行此事时,也会这样说???

DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on this: http://weblog.rubyonrails.org/2012/1/4/rails-3-2-0-rc2-has-been-released. (called from <top (required)> at /var/www/html/sampleapp/Rakefile:7)

1 个答案:

答案 0 :(得分:3)

我也在我的项目中使用jquery,它工作正常

关于生产也

这是我的application.js内容

//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require tinymce
//= require_tree .

我认为添加 // = require_tree。可以解决问题请尝试一次并回复....