简体   繁体   English

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

[英]Adding a jQuery Plugin into a Ruby on Rails App

I'm working on a website that uses HTML, CSS, and JavaScript with jQuery plugins. 我正在使用jQuery插件使用HTML,CSS和JavaScript的网站。 In order to make the website more dynamic, I shifted my code to Ruby on Rails. 为了使网站更加动态,我将代码转移到了Ruby on Rails。 The HTML and CSS are fine, but some of the jQuery plugins that I had working before aren't functioning anymore. HTML和CSS都很好,但是我以前使用的某些jQuery插件不再起作用。

I tried multiple ways to fix this. 我尝试了多种方法来解决此问题。 I first added the javascript files into app/assets/javascripts and then added //= require pluginfilename in application.js and then <%= javascript_include_tag name..... %> but that didn't work. 我首先将javascript文件添加到app/assets/javascripts ,然后在application.js中添加//= require pluginfilename ,然后在<%= javascript_include_tag name..... %>但这没有用。

Is there something that I'm missing or doing wrong? 有什么我想念或做错的事吗? I'm fairly new to Rails. 我是Rails的新手。 This is version 4.2.3 btw. 这是4.2.3 btw版本。

Edit: this is my application.js file 编辑:这是我的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 .

First thing I would do would be to try and find a version of the plugin that's distributed via CDN, then including it via <%= javascript_include_tag link %>. 我要做的第一件事是尝试找到通过CDN分发的插件版本,然后通过<%= javascript_include_tag链接%>包含它。

Barring that... 除非...

Did you do a //= require pluginfilename without the extension? 您是否执行// =要求不带扩展名的pluginfilename? Can you paste in your application.js file so we can take a look? 您可以粘贴到application.js文件中,以便我们看看吗?

Also, what errors are you getting your JS console? 另外,您遇到了JS控制台错误? Are you ensuring that the plugin is being loaded before it's being used? 您是否确定在使用插件之前已将其加载? (Load order in the application.js file matters.) (application.js文件中的加载顺序很重要。)

You can find a bridge between Bower and Rails with Rails-Assets.org . 您可以通过Rails-Assets.org在Bower和Rails之间找到一座桥梁。 I've used this for several projects and it works nicely. 我已经将此用于多个项目,并且效果很好。 If your plugins are using bower, they should exist on Rails-Assets.org 如果您的插件使用Bower,则它们应该存在于Rails-Assets.org上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM