How to Use Rails 5.1.0 and Jquery

How to use Rails 5.1.0 and jQuery

Summary:

  • Install Yarn
  • yarn add jquery
  • Add jquery to application.js manifest file

~~~

I came across this problem today as well.

In this article about using ActionCable with 5.1 I learned that the new way to get JQuery in your app is to use Yarn which is a javascript dependency manager (Think Gemfile and Bundler but for javascript).

You'll notice a new executable file when you create a new app in Rails 5.1: bin/yarn. Trying to run this without yarn installed on your system will lend the typical helpful "what to do next" message:

Download Yarn at https://yarnpkg.com/en/docs/install

  • If you are using homebrew on Mac, you can simply brew install yarn.

  • If you use Chocolatey on Windows, choco install yarn.

  • On Linux, the usual "add a repo and sudo apt-get install yarn apply. I'll let you wander over to the docs page to get the details.

Once you're yarned up, you can do:

yarn add jquery

which will add jquery to the .js dependency file: package.json. This is kind of like your application's "Javascript Gemfile", and Yarn is your "Bundler".

Now that you've got jquery added to your project, you can include it in your manifest the usual way.

//= require jquery
//= require rails-ujs
//= require turbolinks
//= require bootstrap
//= require_tree .

You may also find this link helpful as well. It's an article talking about the new ideas for handling Javascript dependencies within Rails.

Integrating Bootstrap into Ruby on Rails 5.1.4

Have you added the following?

Require Bootstrap Javascripts in app/assets/javascripts/application.js

//= require jquery
//= require bootstrap-sprockets

ruby on rails 5.1 error with devise gem

According to https://github.com/plataformatec/devise/issues/4518 Devise 4.0 doesn't work with Rails 5.1.0

Next week there will be a new release, meanwhile in your Gemfile you can use:

gem "devise", git: 'https://github.com/plataformatec/devise.git'

couldn't find file 'rails-ujs' with type 'application/javascript' (On page load)

gem 'rails-ujs', '~> 5.1.0.beta1'

Include this gem in your gem file
or use rails 5.1.4 that has it by default. Or remove it from

app/assets/javascripts/application.js
require rails-ujs #this line remove

ActionView::Template::Error (couldn't find file )

This fixed the problem for me: I just removed the line

//= require jquery.validate/jquery.validate.min

from frontend.js



Related Topics



Leave a reply



Submit