Rails app does not load a specific JavaScript file - javascript

I currently try to port this template from startbootstrap.com into my rails app. Most of it already works but the navigation bar does not change its color while scrolling.
The different colors are achieved by the creative.js file. I put this file under assets/javascript and included as in my application.js file:
//= require jquery
//= require bootstrap.min
//= require jquery.easing.min
//= require jquery.fittext
//= require wow.min
//= require creative
//= require jquery_ujs
//= require turbolinks
//= require_tree .
I inspected the assets with the Chrome Developer Tools and the creative.js file does not appear there. Since the other javascript files appear in the asset folder the asset pipeline seems to be working.
What do I have to change so that the creative.js file appears in my assets too?

I faced the same issue with my project where I tried to incorporate Bootstrap's creative theme.
The issue for me was that creative.js was not included at the right position (if you notice in the original theme the JS script calls are made at the end of the HTML file, after all the class and id declarations).
Your order of inclusion inside application.js is correct =>
//= require jquery
//= require bootstrap.min
//= require jquery.easing.min
//= require jquery.fittext
//= require wow.min
//= require creative
//= require jquery_ujs
//= require turbolinks
//= require_tree .
However, there is a good chance that application.js is not included at the right spot. Ensure that you are including the files after the below code segment has ended =>
<nav id="mainNav" class="navbar navbar-default navbar-fixed-top">
Best of luck!

Try to delete the coffee file with the same name.
In your case creative.coffee
It worked for me!

Related

difference between require() and //= in application.js

In a Rails application, inside application.js, what does the lines that start with //= require mean and what is the difference with a normal require();?
I'm new at web development, at the beginning I thought this were irrelevant as they appear as commented.
For example, this:
//= require cocoon
//= require jquery_nested_form
//= require jquery3
//= require jquery_ujs
//= require jquery
Rails uses Asset Pipeline(Sprokets) to bundle all js and css files in one and minifies it for better performance of the page loading.
app/assets/javascripts/application.js called manifest file where you mention your javascript files you want to include and the order in which you want to include using
//= require
Similarly for css, you have app/assets/stylesheets/application.css
/* ...
*= require_self
*= require_tree .
*/
require() in context of ruby is to load other file in current file so that it's functionality can be reused.
require in context of javascritp is to load javascript modules to be used in current context/file. It's CommonJS syntaxt. This comes into picture when you use webpacker gem.

Rails Javascript not executing, assets compiled correctly

I am having a issue with javascript not running properly on my production environment. I precompiled my assets with
rake assets:precompile RAILS_ENV=production
before pushing code up to git and heroku but it refuses to execute bootstrap's js functions, google analytics/new relic code, menus, image sliders, etc. Here's my application.js
//= require jquery
//= require bootstrap
//= require jquery_ujs
//= require menu
//= require jquery-ui/autocomplete
//= require autocomplete-rails
//= require jquery-ui/accordion
//= require jquery-ui/tooltip
//= require jquery.raty
//= require search
//= require sameheights
//= require nprogress
//= require sweet-alert
//= require sweet-alert-confirm
//= require select2
//= require ratyrate
//= require local_time
//= require tinymce-jquery
//= require fancybox
//= require_tree .
When I inspect the page, there are no errors or warnings. Some of these functions work correctly on development, but not all. I tried removing each of the lines one by one to see if I could find a culprit but no luck. In fact, since the last time I precompiled assets, the only files that were added were menu, search, and sameheights so everything was working fine beforehand.
Additionally, my production environment correctly sees the minified application.js and application.css files with all the pertinent functions and I have already checked for dupes as well. HELP!
It seems that the issue might be with either jquery or bootstrap. I removed both gems and added cdn hosted versions and it seems to have resolved the issues.

Rails production: javascript not working

Recently I've got strange error on Rails production - some javascripts not working. Locally there is no problem - everything works perfectly, but production.
my application.js
//= require jquery
//= require jquery_ujs
//= require twitter/bootstrap
//= require moment
//= require bootstrap-datetimepicker
//= require pickers
//= require cocoon
//= require jquery.minicolors
//= require widget
//= require highcharts/highcharts
//= require highcharts/highcharts-more
//= require highcharts/highstock
//= require_tree .
scripts that not working - jquery.minicolors and widget(coffescript).
I've tried to add these scripts to
config.assets.precompile += %w( widget.js.coffee jquery.minicolors.js )
in production.rb, but that's doesn't work too
It's strange but if I remove
//= require highcharts/highcharts
//= require highcharts/highcharts-more
//= require highcharts/highstock
from application.js the rest scripts work fine
I understand this issue has been closed but I actually had the same issue recently. I fixed it by wrapping my jQuery code inside the following;
$(document).ready(
(custom JS goes here)
)
If you (or anyone in the future) has the same issue, I'd say it's cause of how Rails loads CSS/HTML and jQuery. Apparently jQuery is loaded as the head in a Rails app so it's loaded but can't be accessed after it's been loaded.
I've found a solution with help of question
I removed from application.js
//= require highcharts/highcharts
//= require highcharts/highcharts-more
and keep only
//= require highcharts/highstock
highstock.js keeps in /vendor/assets/javascripts and contains necessary scripts for highcharts working

Loading javascript file from rails 4 asset pipeline

I'm trying to get a jquery plugin (geocomplete) to work and am having an issue with my rails asset pipeline.
To get the plugin to work, I need to load a .js file called jquery.geocomplete.js located in my /vendor/assets/javascripts/ directory
In my application.js file I include jquery.geocomplete
//= require jquery
//= require jquery_ujs
//= require bootstrap.min
//= require turbolinks
//= require jquery.turbolinks
//= require jquery.geocomplete
//= require jquery.geocomplete.min
//= require underscore
//= require gmaps/google
//= require_tree .
When I load my app in development mode, I can see in the source code that the following asset has been loaded:
<script data-turbolinks-track="true" src="/assets/jquery.geocomplete.js?body=1"></script>
However, the plugin requiring does not work. To get it to work I have to paste:
<script src="/assets/jquery.geocomplete.js"></script>
into my relevant view. (This then causes issues when I run in production mode in Heroku)
Can anyone tell me why the myjquery.geocomplete.js file will not run my plugin when I load it through the asset pipeline?
Any thoughts/ideas appreciated.

The bootstrap.js file - are these a unique type of server-side include? How should these files be implemented?

In the bootstrap sass download ( https://github.com/twbs/bootstrap-sass/archive/v3.1.1.tar.gz ) there is a strange bootstrap.js file with all of these lines here commented out, like so:
//= require bootstrap/affix
//= require bootstrap/alert
//= require bootstrap/button
//= require bootstrap/carousel
//= require bootstrap/collapse
//= require bootstrap/dropdown
//= require bootstrap/tab
//= require bootstrap/transition
//= require bootstrap/scrollspy
//= require bootstrap/modal
//= require bootstrap/tooltip
//= require bootstrap/popover
These correspond to the folder below that has the js files named by each "require" line.
Are these some sort of special server side include system for javascript?
While we are on the subject, what is the best practice for including these files in a page? 1 by 1 as they are needed (more requests?) or simply pasting them into a master file as needed (more work indeed, and subject to change, more stuff to track).
Perhaps there is something I am missing here.
These JS files are intended to be loaded/compiled by Sprockets.
If you are not using Rails, look at a solution for sinatra: https://gist.github.com/datenimperator/3668587

Categories

Resources