Reputation: 13
I've looked over the internet and found some instructions for this question:
Using jQuery plugins in Rails 3
http://ridingwithrails.wordpress.com/2011/12/28/rails-3-1-installing-lightbox-jquery-plugin/
Here is exactly what I do to install a Jquery plugin in rails. I can never get access to the new methods in the console. (I get the error, the function doesn't exist)
1 - I Download the plugin and put it in:
vendor/assets/javascripts/plugin.js
2 - I add the path to the file in application.js:
//= require jquery
//= require jquery_ujs
//= require plugin
//= require_tree .
3 - I do the same for the css if it applies. (in the css folders)
Some plugins I've tried that with are: jquery-cookie jquery-star-rating But it never works. What I'm I doing wrong?
Thanks for you answers
EDIT: When I check the source of my views the *.js files of the plugins are included but whenever I try to use a function from those plugins I get for example:
[17:18:59,927] TypeError: $.cookie is not a function
Upvotes: 0
Views: 2941
Reputation: 7403
Try the following to help debug where the issue is precisely:
Define a global function at the top of the plugin.js file and try calling it from the console. (you could just call an alert() here with similar results). If you're able to call this function, then the plugin file is being included as you claim. If not, then it's possible your assets path is off, so try adding a line like this to your config/application.rb
config.assets.paths << Rails.root.join("Vendor", "assets", "javascripts")
Open your page in chrome or firebug with the javascript console open, and ensure that there are no javascript errors. It's possible that an error is preventing the plugin from loading correctly.
Try using the simplest of plugins (like from http://docs.jquery.com/Plugins/Authoring) and if that works, then the issue is with the specific plugin you're trying to use.
Try wrapping your plugin inside a document ready block, as it may require the DOM to be in place (note: this isn't always recommended, as it can cause an unoptimized page load, but it's worth a try)
$(function() {
// put the plugin code here
});
Are you loading an other javascripts that use $
in their namespace, like prototype?
Is this a vanilla rails app, or a large, complex app? If it's the latter, see if you can reproduce the issue in a minimal failing example.
Upvotes: 4