将jQuery插件添加到Ruby on Rails应用程序中

时间:2015-08-18 19:08:00

标签: javascript jquery css ruby-on-rails ruby

我正在开发一个使用HTML,CSS和JavaScript与jQuery插件的网站。为了使网站更具动态性,我将代码转移到Ruby on Rails。 HTML和CSS很好,但我以前工作的一些jQuery插件不再起作用了。

我尝试了多种方法来解决这个问题。我首先将javascript文件添加到app/assets/javascripts,然后在application.js中添加//= require pluginfilename,然后在<%= javascript_include_tag name..... %>中添加{但不起作用。

我有什么遗漏或做错了吗?我是Rails的新手。这是版本4.2.3 btw。

编辑:这是我的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 any plugin's vendor/assets/javascripts directory 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/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap.min
//= require turbolinks
//= require countdown
//= require grid
//= require custom
//= require script
//= require_tree .

2 个答案:

答案 0 :(得分:1)

我要做的第一件事就是尝试找到通过CDN分发的插件版本,然后通过&lt;%= javascript_include_tag link%&gt;包含它。

除非......

你做了没有扩展名的// = require pluginfilename吗?你可以粘贴在你的application.js文件中,以便我们看看吗?

此外,您获得JS控制台的错误是什么?你确定插件在被使用之前是否正在加载? (application.js文件中的加载顺序很重要。)

答案 1 :(得分:1)

您可以使用Rails-Assets.org在Bower和Rails之间找到一座桥梁。我已经将它用于了几个项目并且运行良好。如果您的插件使用的是bower,它们应该存在于Rails-Assets.org