Reputation: 1051
Rails beginner here !
I'm trying to load .js files through the asset pipeline. As mentioned in Rails Guides:
2.2.1 Search Paths
When a file is referenced from a manifest or a helper, Sprockets searches the three default asset locations for it.
The default locations are: the
images
,javascripts
andstylesheets
directories under theapp/assets
Whenever I run rails new my_project
, Rails doesn't generate the app/assets/javascripts
directory, nor the related manifesto:
app/assets/
├── config
│ └── manifest.js
├── images
└── stylesheets
└── application.css
I've manually added the /app/assets/javascripts
directory and the application.js
file. Here is its content:
//= require mytheme/js/test # a simple alert('test')
//= require rails-ujs
//= require turbolinks
//= require_tree .
//= require mytheme/js/file1 # for each file of my theme
but no alert pops up. However, using the 'Network' tab of the browser's Inspect Mode shows me that there's a .js file (nothing related to my theme though).
Should I conclude that Rails is using another manifesto to load .js files ? Pragmatically, how could I ask the Asset Pipeline to load specific .js files ?
Long post of a beginner question, thanks for your time.
Rails.application.config.assets.paths << Rails.root.join('vendor')
to config/initializers/assets.rb
Upvotes: 4
Views: 3136
Reputation: 3002
You likely have the default javascript_pack_tag
in your application layout, which is the new default in Rails 6 with webpacker.
<%= javascript_pack_tag 'application' %>
In order to use the js you have added you need the javascript_include_tag
tag. Like in previous versions of Rails.
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
That should load those js files.
Upvotes: 2