2 years ago
#3691
Ioana Marusea Nicoară
Rails - jQuery doesn’t recognise '$' and it doesn’t show changes from new .js files
I'm coding my first Ruby on Rails app (after this tutorial https://www.freecodecamp.org/news/lets-create-an-intermediate-level-ruby-on-rails-application-d7c6e997c63f/), but I've encountered some issues.
When I add new JavaScript files the changes don’t show. I've tried to move all the js code into application.js and the console says that
"Uncaught ReferenceError: $ is not defined at Object../app/javascript/packs/application.js (application.js:34) at
__webpack_require__
(bootstrap:19) at bootstrap:83 at bootstrap:83."
This is the code I added to application.js:
$(document).on('turbolinks:load', function() {
if ($(".single-post-card").length) {
// set a solid background color style
if (mode == 1) {
$(".single-post-card").each(function() {
$(this).addClass("solid-color-mode");
$(this).css('background-color', randomColor());
});
}
// set a border color style
else {
$(".single-post-card").each(function() {
$(this).addClass("border-color-mode");
$(this).css('border', '5px solid ' + randomColor());
});
}
}
$('#feed').on( 'mouseenter', '.single-post-list', function() {
$(this).css('border-color', randomColor());
});
$('#feed').on( 'mouseleave', '.single-post-list', function() {
$(this).css('border-color', 'rgba(0, 0 , 0, 0.05)');
});
});
var colorSet = randomColorSet();
var mode = Math.floor(Math.random() * 2);
// Randomly returns a color scheme
function randomColorSet() {
var colorSet1 = ['#45CCFF', '#49E83E', '#FFD432', '#E84B30', '#B243FF'];
var colorSet2 = ['#FF6138', '#FFFF9D', '#BEEB9F', '#79BD8F', '#79BD8F'];
var colorSet3 = ['#FCFFF5', '#D1DBBD', '#91AA9D', '#3E606F', '#193441'];
var colorSet4 = ['#004358', '#1F8A70', '#BEDB39', '#FFE11A', '#FD7400'];
var colorSet5 = ['#105B63', '#FFFAD5', '#FFD34E', '#DB9E36', '#BD4932'];
var colorSet6 = ['#04BFBF', '#CAFCD8', '#F7E967', '#A9CF54', '#588F27'];
var colorSet7 = ['#405952', '#9C9B7A', '#FFD393', '#FF974F', '#F54F29'];
var randomSet = [colorSet1, colorSet2, colorSet3, colorSet4, colorSet5, colorSet6, colorSet7];
return randomSet[Math.floor(Math.random() * randomSet.length )];
}
// Randomly returns a color from an array of colors
function randomColor() {
var color = colorSet[Math.floor(Math.random() * colorSet.length)];
return color;
}
I've tried to change the way I'm writing the application.js as shown below:
//= require jquery
//= require jquery_ujs
//= require bootstrap/dropdown
//= require bootstrap-sprockets
//= require turbolinks
//= require_tree
//= require.js
//import Rails from "@rails/ujs"
//import Turbolinks from "turbolinks"
//import * as ActiveStorage from "@rails/activestorage"
//import "channels"
//Rails.start()
//Turbolinks.start()
//ActiveStorage.start()
//import "bootstrap";
//require("@rails/ujs").start()
//require("turbolinks").start()
//require("@rails/activestorage").start()
//require("channels")
//require('jquery')
//require("posts/modal")
//require("posts/style")
I've also tried to move the javascript folder from app to app/assets. All the gems are up to date.
What can I do?
javascript
jquery
ruby-on-rails
ruby
turbolinks
0 Answers
Your Answer