Vlad
Vlad

Reputation: 61

Rails asset pipeline doesn't load javascript dependencies

I've been following Michael Hartl's Ruby on Rails tutorial and I am having trouble loading JavaScript dependencies as part of the 8th chapter: https://www.railstutorial.org/book/basic_login

The CSS dependencies load fine (I'm using bootstrap), and upon inspecting the page in the browser there are multiple <link rel="stylesheet...> tags in the head. However, there is only one script tag, which loads the compiled application.js file.

The interesting thing is, even if I make mistakes in the js manifest file, such as requiring a file that does not exist, I do not get any errors when rendering my pages in a browser.

I've tried reproducing this in a fresh app, but I was unable to. Simply requiring the needed gems in the Gemfile, bundle install and visiting any page than the default rails one, will include all dependencies listed in the application.js manifest file as separate <script src=..> tags in the <head>.

I'm loading the assets as part of the layouts/application.html.erb file, using the helpers:

<%= csrf_meta_tags %>
<%= stylesheet_link_tag    'application', media: 'all',
                                    'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

application.js manifest 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. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require turbolinks
//= require_tree .

My RoR and Ruby versions are 5.1 and 2.3.2 respectively.
The js manifest file is app/assets/javascripts/application.js

Upvotes: 3

Views: 426

Answers (1)

Vlad
Vlad

Reputation: 61

I solved the issue by moving the relevant parts of my main app into a fresh rails app that did load the JS dependencies. I expect this to have been a configuration problem on my side.

More specifically, I moved the Gemfile, db/, config/routes.rb and everything from app/ (except config/) into a fresh rails app. Once everything worked I overwrote my main application's app/ directory with the one from the fresh app.

Upvotes: 2

Related Questions