Import maps let you use JavaScript modules in Rails without needing extra build tools. They help your app load JavaScript files directly in the browser.
Import maps (Rails 7+)
importmap pin "module-name" [to: "url-or-path"] // In your JavaScript file: import { something } from "module-name" // In your Rails layout: <%= javascript_importmap_tags %>
The importmap pin command registers a JavaScript module with a name and optional URL or path.
Use javascript_importmap_tags helper in your layout to load the import map and modules.
importmap pin "application" importmap pin "lodash", to: "https://cdn.skypack.dev/lodash"
debounce function from the pinned "lodash" module and uses it.// In app/javascript/application.js import { debounce } from "lodash" debounce(() => console.log('Hello'), 300)
<%= javascript_importmap_tags %>
This example shows how to pin modules in importmap.rb, import and use a function from lodash in application.js, and include the import map tags in the Rails layout. When you click the button, it alerts "Clicked!" with a debounce delay.
// config/importmap.rb importmap pin "application" importmap pin "lodash", to: "https://cdn.skypack.dev/lodash" // app/javascript/application.js import { debounce } from "lodash" window.onload = () => { const button = document.getElementById('btn') button.addEventListener('click', debounce(() => alert('Clicked!'), 500)) } // app/views/layouts/application.html.erb <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Importmap Demo</title> <%= javascript_importmap_tags %> </head> <body> <button id="btn">Click me</button> </body> </html>
Import maps work best for small to medium JavaScript needs without complex bundling.
You can update pinned modules by running bin/importmap pin commands.
Remember to restart your Rails server after changing importmap configuration.
Import maps let Rails apps load JavaScript modules without bundlers.
Use importmap pin to register modules and javascript_importmap_tags to load them.
This keeps JavaScript simple and easy to manage in Rails 7+.
Practice
importmap in Rails 7+?Solution
Step 1: Understand importmap's role in Rails 7+
Import maps allow Rails apps to load JavaScript modules directly in the browser without bundling tools.Step 2: Compare other options
Options B, C, and D describe unrelated tasks: compiling Ruby, managing migrations, and styling, which are not importmap's purpose.Final Answer:
To load JavaScript modules without needing a bundler -> Option AQuick Check:
Import maps = load JS modules without bundler [OK]
- Confusing importmap with asset bundlers
- Thinking importmap compiles Ruby code
- Mixing importmap with CSS or database tasks
chart.js using importmap in Rails?Solution
Step 1: Identify the correct command syntax
The correct command to pin a module in Rails 7+ isbin/importmap pin [module].Step 2: Check other options
Options B, C, and D are incorrect: B uses an incorrect verbadd, C uses invalidrails importmap pin, and D misses thebin/prefix.Final Answer:
bin/importmap pin chart.js -> Option BQuick Check:
Pin module command = bin/importmap pin [name] [OK]
- Using 'rails importmap' instead of 'bin/importmap'
- Using 'add' instead of 'pin'
- Omitting 'bin/' prefix in the command
<%= javascript_importmap_tags %>
What does this helper do in a Rails 7+ app using import maps?
Solution
Step 1: Understand the helper's purpose
javascript_importmap_tagsinserts script tags to load all pinned JS modules via import maps in the HTML.Step 2: Eliminate incorrect options
It does not bundle, remove unused code, or generate CSS styles.Final Answer:
It loads all pinned JavaScript modules into the HTML page -> Option DQuick Check:
javascript_importmap_tags = load pinned JS modules [OK]
- Thinking it bundles JS files
- Assuming it cleans unused code
- Confusing it with CSS helpers
rails importmap pin my_module but your JavaScript module does not load in the browser. What is the most likely mistake?Solution
Step 1: Check how import maps load modules
Pinning registers the module, butjavascript_importmap_tagsmust be in the layout to load scripts.Step 2: Review other options
You usedpininstead ofaddcommand is incorrect becausepinis the right command. You need to restart the Rails server after pinning is unnecessary; no restart needed. You must compile assets with webpack first is wrong because import maps do not require webpack.Final Answer:
You forgot to add <%= javascript_importmap_tags %> in your layout -> Option AQuick Check:
Missing javascript_importmap_tags = no JS load [OK]
- Not including javascript_importmap_tags in views
- Confusing pin with add command
- Thinking server restart is required
- Assuming webpack is needed with import maps
config/importmap.rb?Solution
Step 1: Understand pin syntax for external URLs
To pin a module from a CDN, usepin 'name', to: 'URL'inconfig/importmap.rb.Step 2: Check other options
pin 'library', to: '/assets/library.js' uses a local path, not CDN. Options C and D use incorrect keysfromandurlwhich are invalid.Final Answer:
pin 'library', to: 'https://cdn.example.com/library.js' -> Option CQuick Check:
Pin CDN module = pin 'name', to: 'URL' [OK]
- Using wrong keys like 'from' or 'url'
- Pinning local path instead of CDN URL
- Confusing syntax for pinning external scripts
Ready to prove it?
Pass the test and this pattern moves unknown
