I want to use a simple jQuery function like so:
$( document ).ready(function(){
$('body').addClass("faded");
});
From a single view in a rails 5.1.3 app, in this case 'login.html.erb'
On webpack's application.js I have:
var $ = require('jquery');
which makes the aforementioned jquery call work IF I put it on the same application.js file but this would mean it would be called for all the pages since <%= javascript_pack_tag 'application' %> is on the layout.
When I try to run it on login.html.erb like so:
<script type="text/javascript">
$( document ).ready(function(){
$('body').addClass("faded");
});
</script>
I get an error on console: "ReferenceError: $ is not defined".
If I try to do " import $ from 'jquery'; " on the login.html.erb file I get this error instead: "SyntaxError: import declarations may only appear at top level of a module" which understandably means that my local .erb view doesn't get access to the javascript referenced on webpacker's application.js and cant import it from there.
How can I reference jquery and for that matter any module being served by webpacker, from the views?
I apologize if this has been asked before, I'm posting this question after days of reading about webpack, the webpacker gem and javascript without finding a solution.
:)