I am having a lot of trouble getting any javascripts to run in my Ruby application.
Currently my console is giving me one error message:
"Uncaught TypeError: Cannot read property 'clientWidth' of undefined"
This error is referencing this line in my application.js file:
const size = carouselImages[0].clientWidth;
I'm guessing it's saying it has no idea what clientWidth is? I was following along with this tutorial when I hit this roadblock. (He's not making a rails app, but his script loads fine.) The script also loaded fine for me when I created a regular index.html page not tied to my ruby app.
How do I define clientWidth/do I need to?
My Rails files:
Gem File
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem 'jquery-rails', '~> 4.3', '>= 4.3.3'
gem 'rails-ujs', '~> 0.1.0'
gem 'jquery-ui-rails'
_header.html.erb
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Page Title</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag( 'slider.js' ) %>
</head>
<body> <!--Body and html tags are closed in my _footer.html.erb -->
application.js
//= require rails-ujs
//= require jquery
//= require jquery_ujs
//= require turbolinks
$(document).ready to $(document).on('turbolinks:load', function(main());
//image slider code
//
//
const carouselSlide = document.querySelector('.carousel-slide');
const carouselImages = document.querySelectorAll('.carousel-slide img');
//buttons
const prevBtn = document.querySelector('#prevBtn');
const nextBtn = document.querySelector('#nextBtn');
//Counter
let counter = 1;
const size = carouselImages[0].clientWidth;
carouselSlide.style.transform = 'translateX('+(-size * counter)+ 'px)';
slider.html.erb
<button id="prevBtn">Prev</button>
<button id="nextBtn">Next</button>
<br /><br />
<div class="carousel-container">
<div class="carousel-slide">
<%= image_tag("photo4.jpg", id: "lastClone") %>
<!-- photo1.jpg is an image of a road at sunset should have the border around it and be the focus. Currently, it is not. -->
<%= image_tag("photo1.jpg") %>
<%= image_tag("photo2.jpg") %>
<%= image_tag("photo3.jpg") %>
<%= image_tag("photo4.jpg") %>
<%= image_tag("photo1.jpg", id: "firstClone") %>
</div>
</div>
slider.scss
/* This is for a test slider */
.carousel-container {
width: 1000px;
border: 5px solid black;
}
.carousel-slide {
display: flex;
img{
width: 1000px;
height: auto;
}
}
Here is an imgur link to see a screenshot of my ruby app vs. a regular .html file I created to compare/contrast what is going on: https://imgur.com/a/jVhjLd3
Here are the versions I am running of things:
Ruby: ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]
Node: v13.2.0
NPM: 6.13.1
I tried a few different suggestions found via google searches (a la 'get fixed quick'), so if anything is off or out of place, please point it out to me. If you require more info/code, I will be happy to provide.
Any help getting my Javascript to run would be greatly appreciated!