I'm working on a website that uses HTML, CSS, and JavaScript with jQuery plugins. In order to make the website more dynamic, I shifted my code to Ruby on Rails. The HTML and CSS are fine, but some of the jQuery plugins that I had working before aren't functioning anymore.
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.
Is there something that I'm missing or doing wrong? I'm fairly new to Rails. This is version 4.2.3 btw.
Edit: this is my application.js file
// 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 %>.
Barring that...
Did you do a //= require pluginfilename without the extension? Can you paste in your application.js file so we can take a look?
Also, what errors are you getting your JS console? Are you ensuring that the plugin is being loaded before it's being used? (Load order in the application.js file matters.)
You can find a bridge between Bower and Rails with Rails-Assets.org. I've used this for several projects and it works nicely. If your plugins are using bower, they should exist on Rails-Assets.org
Related
I am struggling.
I have rails 5 app with a series of js files in my app/assets/javascripts folder. The js in those files works just fine in the development environment, but when I publish on heroku, it stops working.
I can see from the chrome inspector, that the application.js file that is visible from the network tab shows the content of the js files in my app. That js just doesnt seem to do anything in production. No console errors appear from the chrome console inpsector.
I can see from this post that there are a series of things to try. I've tried all of them. I've even made a completely new app and added the js files on at a time to see if there is a tipping point where something is required in a different order to something else (mostly guessing) - but that does nothing to fix the problem.
I can see from this article that one suggestion is to:
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
and then try:
rake assets:clean assets:precompile
I've tried each of these suggestions and still can't find the problem.
If there were an error in the way I've written the js, I would expect that to be exposed in both my development and production environment console inspectors in chrome. There are no errors showing in either environment.
If there were a problem with the production environment reading the js file, I'm confused about why I can word search for the words used in the js file in the file I can open from the network tab and see the correct text.
How do I go about solving this problem?
My relevant files are:
app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require jquery-fileupload/vendor/jquery.ui.widget
//= require jquery-fileupload/jquery.iframe-transport
//= require jquery-fileupload/jquery.fileupload
//= require bootstrap-sprockets
//= require moment
//= require bootstrap-datetimepicker
//= require pickers
// require underscore
// require gmaps/google
//= require markerclusterer
//= require cocoon
//= require cable
//= require_tree .
config/initializers/assets.rb
# Be sure to restart your server when you modify this file.
# Version of your assets, change this if you want to expire all your assets.
Rails.application.config.assets.version = '1.0'
# Add additional assets to the asset load path
# Rails.application.config.assets.paths << Emoji.images_path
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
# Rails.application.config.assets.precompile += %w( search.js )
config/production.rb
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false
The relevant file that is not working in the production environment is saved in app/assets/javscripts/stance/commercial.js (so it should be identified in application.js by //= require_tree.
That file has:
jQuery(document).ready(function() {
jQuery('[name="organisation[commercial_attributes][standard_financial_split]"]').on('click', function() {
if (jQuery(this).val() == 'true' ) {
jQuery('#commercial_standard_financial_split_content').removeClass('hidden');
} else {
jQuery('#commercial_standard_financial_split_content').removeClass('hidden').addClass('hidden');
}
});
});
Can anyone help with a process for getting js to work in the production environment?
I am out of suggestions for things to try and this isn't the sort of thing that the tutorials, code schools (I am a student in code school, go rails, udemy and have a million books), but can't find an reference point from which to start in solving this problem.
For me, the solution was to remove the javascript include tag from the header of my application.html.erb to beneath the body. That fixed the problem with the js file I referenced in my question.
However, this has now broken my google maps javascript, so I'm not sure that this is actually a good solution.
I have a custom javascript file which i am loading in my layout.
custom.js
//= require jquery
//= require jquery_ujs
//= require modules
//= require login
//= require user
I am loading in my layout as below. The js is getting loaded in a minified form, it seems some of jQuery calls/events are not happening at all. I am not sure whether its because of minification of files.
<%= javascript_include_tag '/assets/custom' %>
I am not sure how to debug this either. Previously these files were loaded from application.js and none of the files were minified in big js instead were loaded separately and everything was working fine.
Now i am trying to load the custom.js file only for certain modules so loaded the corresponding js files in custom.js, but because of the minification its not working or i am not sure whats the problem.
In my production.rb file, below is the configuration
# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
I cannot get any javascript to run when my Ruby on Rails project is in deployment mode. I have a js at vendor/assets/javascript/theme/index-slider.js. It has an alert in this file to test that the javascript is working:
alert('TestTestTest');
When I run the project in the development mode, all the javascript runs properly. Additionally, if I add <script>alert("Testme");</script> to my index.html.erb file, that javascript runs in production mode.
I have been running rake assets:precompile and RAILS_ENV=production rake assets:precompile when making changes as well as restarting the server
How can I get Javascript to run in Production mode? (Also I am rather new to Ruby on Rails so I am not sure which files/information I should include to be helpful.) Thanks.
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 jquery.turbolinks
//= require bootstrap/bootstrap
//= require theme/bootstrap.min
//= require theme/flexslider
//= require theme/index-slider
//= require theme/jquery.countdown.min
//= require theme/jquery.isotope.min
//= require theme/theme
//= require_tree .
I've found that the issue lies in the application.js file. I've added bootstrap into my project by hand as well as a boostrap theme. I've found that for whatever reason, the //= require bootstrap/bootstrap line should not be included above the theme I have chosen.
You need to compile assets for your production as well
rake assets:precompile RAILS_ENV=production
I'm trying to create another manifest file, called app/assets/javascripts/base.js that looks roughly like this:
//= require jquery
//= require jquery_ujs
//= require_tree ./backbone/tempaltes
My plan is to include this before my app/assets/javascripts/application.js file, which has my main backbone code, as such:
//= require ./backbone/app
// etc...
My thinking is that I can cache the stuff that won't change much (my templates, jquery, plugins, etc.) and cache-bust my app code, which will likely change quite frequently, reducing the page size to the client.
I'm requiring both files in my app/views/layouts/application.html.haml like so:
= javascript_include_tag "base"
= javascript_include_tag "application"
However, when I load up my developemnt server, only the files from application.js are loaded; none of the files in base.js are loaded. I've added this line in config/application.rb:
config.assets.precompile += %w(base.js)
... but that doesn't get any of the assets required in base.js to load in development. What do I have to do to get these files to load?
In my rails 3 app it has:
//= require jquery
//= require jquery_ujs
//= require_tree .
//= require jquery-ui
And it works fine. But I don't understand how it works. I think I understand the third line which I believe adds everything to this file that is found in the same directory as this file (/app/assets/javascript). But what about the first line? Where does it get the jquery file? I don't see it in any of the directories it mentions in the comment at the beginning of the file, specifically:
// 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.
Where does rails go to get the jquery javascript file?
The require part you mentioned above is called asset pipeline, which is part of new features of rails 3. The goal of this is to concatenate all javascripts file together, so you have your page load faster by a single import of the javascript file.
You can find out more about asset pipeline here and if not mistaken it is utilizing sprockets gem.
Refering to //= require jquery, it is importing the javascript file from your jquery gem (only if you using jQuery gem). You can find it from your jQuery gem assets folder.
Refer this screencast as it described best.
Hope it helps.
require_tree . includes all files under the directory it is in (e.g. app/assets/javascript). The jQuery source file come from the jquery-rails gem
The comment can be a little confusing. A gem is also called a "plugin" in this case. It gets from the jquery-rails gem. Note the vendor/assets/javascripts structure.
https://github.com/rails/jquery-rails/tree/master/vendor/assets/javascripts