Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 7 with importmap #289

Open
mohloko opened this issue Sep 30, 2022 · 3 comments
Open

Rails 7 with importmap #289

mohloko opened this issue Sep 30, 2022 · 3 comments

Comments

@mohloko
Copy link

mohloko commented Sep 30, 2022

How I can make it work?

@jubilee2
Copy link

I spent whole afternoon learned how to import jquery in Rails 7

edit in Gemfile and run bundle install

gem 'jquery-rails'

edit application.js

// app/javascript/application.js
// jquery
import 'jquery'
import 'jquery_ujs'

edit importmap.rb

# config/importmap.rb

pin "jquery", to: "jquery.min.js", preload: true
pin "jquery_ujs", to: "jquery_ujs.js", preload: true

edit config/initializers/assets.rb

# config/initializers/assets.rb


Rails.application.config.assets.precompile += %w( jquery.min.js jquery_ujs.js )

finally should work

@rahulbir
Copy link

and if you want a specific version of jQuery (2 or 3):

// app/javascript/application.js

import 'jquery'
import 'jquery_ujs'
# config/importmap.rb

pin "jquery", to: "jquery3.min.js", preload: true
pin "jquery_ujs", to: "jquery_ujs.js", preload: true
# config/initializers/assets.rb

Rails.application.config.assets.precompile += %w( jquery3.min.js jquery_ujs.js )

@ArturT
Copy link

ArturT commented Oct 19, 2023

The solution is working.

If you use Sprockets 4.x then it's recommended to use app/assets/config/manifest.js instead of Rails.application.config.assets.precompile.

# app/assets/config/manifest.js
//= link jquery.min.js
//= link jquery_ujs.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants