How to use custom jQuery in Rails 6

How to use custom jQuery in Rails 6

rails 6 jquery is not defined
rails 6 jquery-ui
jquery-rails
bootstrap rails 6
rails jquery is not defined
rails 6 webpacker
rails 6 without webpacker
rails add javascript to view

I've been thinking about this for a couple of days already.

For some reason my semantic-ui jQuery doesn't work.

So here's what I did.

On my webpack/environment.js:

const { environment } = require('@rails/webpacker')

const webpack = require('webpack')
environment.plugins.append('Provide', new webpack.ProvidePlugin({
  $: 'jquery',
  jQuery: 'jquery'
}))

module.exports = environment

I added a custom.js file with the following codes under app/javascripts/packs:

$(document).on('turbolinks:load', function(){
    $('.ui.dropdown').dropdown();
})

And then inside of my application.js w/c is under app/javascripts/packs I have the ff:

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")

require("packs/custom")

On chrome console here's what I see:

Uncaught TypeError: $(...).dropdown is not a function
    at HTMLDocument.<anonymous> (custom.js:3)
    at HTMLDocument.dispatch (jquery.js:4588)
    at HTMLDocument.elemData.handle (jquery.js:4408)
    at Object../node_modules/turbolinks/dist/turbolinks.js.e.dispatch (turbolinks.js:75)
    at r.notifyApplicationAfterPageLoad (turbolinks.js:994)
    at r.pageLoaded (turbolinks.js:948)
    at turbolinks.js:872

I thought I did it right but for some reason this solution did not work at all. Any idea what am I doing wrong?


Look my /confog/webpack/environment.js file like below:

const { environment } = require('@rails/webpacker')

const webpack = require('webpack')
environment.plugins.prepend('Provide',
    new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        Popper: ['popper.js', 'default']
    })
)

module.exports = environment

And /app/javascript/packs/application.js

require("jquery")
require("bootstrap")
require("packs/custom") // Here is my custom jQuery file like packs/custom.js

and in the /packs/custom.js look like for jQuery preloader

// Preloader JS
jQuery(window).on('load', function () {
    $('.preloader').fadeOut();
});

and exactly this way, custom jQuery is working nicely.

My project based on bootstrap 4 so I run this command for installing dependencies

yarn add bootstrap jquery popper.js 

I think it will help.

Thanks

How to use custom jQuery in Rails 6, Look my /confog/webpack/environment.js file like below: const { environment } = require('@rails/webpacker') const webpack  No more using jquery-rails gem!!! Now, How we will use Jquery in Rails 6?? In our Rails application run below command to add jQuery. $ yarn add jquery. It will save the dependency of jquery to our application. Now to verify jquery is installed or not, check below files package.json => "jquery": "^3.3.1", yarn.lock => jquery@^3.3.1:


yarn add jquery

in packs/application.js

import $ from 'jquery';
window.jQuery = $;
window.$ = $;

Introducing jQuery in Rails 6 Using Webpacker, In our Rails application run below command to add jQuery. It will save the dependency of jquery to our application. The path 'jquery/src/jquery' indicates the jquery installed in node_modules directory having sub directory 'jquery/src' and which contains jquery. js file there. How to use custom jQuery in Rails 6. Report spam. Joan Gabun asked October 14, 2019 12:01pm in Rails. Joan Gabun 900 XP · October 14, 2019 12:01pm.


On my webpack/environment.js:

 module.exports = environment
const webpack = require('webpack')
environment.plugins.prepend('Provide',
  new webpack.ProvidePlugin({
    $: 'jquery/src/jquery',
    jQuery: 'jquery/src/jquery'
  })
)

And /app/javascript/packs/application.js

require("jquery")              //add this in application.js
require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")

run this command for installing dependencies

  "yarn add jquery"

Adding Custom JavaScript in Rails 6 - DEV, In Rails 6, JavaScript assets are no longer included with the app/assets/ Method 1: Create a custom directory and require it in application.js so in case your file requires jquery and has name custom.js, with following order:. To do so, add a custom file within app/javascript/packs, e.g. custom.js, then use the javascript_pack_tag helper where the file is needed in the views like so: <%= javascript_pack_tag 'custom' %> The custom javascript will be compiled separately from the rest of the javascript into a timestamped custom.js that looks something like this: custom-a03d1756c0a3f825e53e.js


How to write Javascript in Rails 6, In a Rails 6 application, when you need a Javascript library, you: If you need to use a library (select2 or jQuery for instance), would it work to  jQuery; Popper.js; Bootstrap needs both jQuery and Popper.js in order for Javascript to function properly. The big difference between Rails 5 and Rails 6 at this point is that Rails 6 does not require you to add these as gems to your Gemfile. All you need is Yarn and the Webpacker gem (that Rails 6 comes with).


How to install Bootstrap and jQuery on Rails 6, How to install Bootstrap and jQuery on Rails 6 You may also have to require your custom JS files in the application.js file. I hope all this works  The jquery-rails gem was removed as a dependency starting Rails 5.1. It does not mean that jQuery is not recommended to be used. jQuery as a default library is mainly used for Unobtrusive JavaScript which we'll take more about in a future post.


How to "load" jQuery plugins with rails 6 and webpacker? : rails, I did install the packages like: yarn add jquery yarn add chart.js yarn add simplebar yarn add jquery-scroll-lock. This is my webpacker environment file: I built Super Graph an automatic GraphQL to SQL translator (in GO). Just run it along side your Rails app it will learn your database schema, relationships and allow you to query your database using just simple GraphQL. Super Graph understands Rails cookies and works with session stores to get the current authenticated users ID.