📖 Scenario: You are building a simple Rails 7 application that uses import maps to manage JavaScript modules without a bundler. This helps you load JavaScript files easily and keep your app organized.
🎯 Goal: Create a Rails 7 app setup that uses import maps to load a custom JavaScript module and display a greeting message on the page.
📋 What You'll Learn
Create a JavaScript module file with a greeting function
Add the JavaScript module to the import map configuration
Import and use the module in the application JavaScript
Include the JavaScript in the Rails layout to run on page load
💡 Why This Matters
🌍 Real World
Import maps let Rails apps manage JavaScript modules without complex bundlers, making it easier to add and update JS code.
💼 Career
Understanding import maps is important for modern Rails developers to efficiently organize and load JavaScript in their applications.
Progress0 / 4 steps
1
Create a JavaScript module with a greeting function
Create a file called app/javascript/greeting.js with a function named sayHello that returns the string 'Hello from import maps!'. Export the function as a named export.
Ruby on Rails
Hint
Use export function sayHello() to define and export the function.
2
Add the greeting module to the import map
Add the greeting module to the import map by running the command bin/importmap pin greeting in your terminal. Then, open config/importmap.rb and verify it contains pin "greeting", to: "greeting.js".
Ruby on Rails
Hint
Use the bin/importmap pin greeting command to add the module to the import map.
3
Import and use the greeting module in application.js
In app/javascript/application.js, import the sayHello function from the greeting module. Then, call sayHello() and assign its result to a constant named message.
Ruby on Rails
Hint
Use import { sayHello } from "greeting" to import the function.
4
Display the greeting message on the page
In app/views/layouts/application.html.erb, add the javascript_importmap_tags helper inside the <head> tag to load the JavaScript. Then, in app/javascript/application.js, add code to insert the message text inside an element with id greeting when the page loads.
Ruby on Rails
Hint
Use <%= javascript_importmap_tags %> in the HTML head and add a DOMContentLoaded event listener in JavaScript.
Practice
(1/5)
1. What is the main purpose of using importmap in Rails 7+?
easy
A. To load JavaScript modules without needing a bundler
B. To compile Ruby code into JavaScript
C. To manage database migrations automatically
D. To style HTML elements with CSS
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 A
Quick Check:
Import maps = load JS modules without bundler [OK]
Hint: Import maps = JS modules without bundler [OK]
Common Mistakes:
Confusing importmap with asset bundlers
Thinking importmap compiles Ruby code
Mixing importmap with CSS or database tasks
2. Which command correctly registers a JavaScript module named chart.js using importmap in Rails?
easy
A. rails importmap add chart.js
B. bin/importmap pin chart.js
C. rails importmap pin chart.js
D. importmap pin chart.js
Solution
Step 1: Identify the correct command syntax
The correct command to pin a module in Rails 7+ is bin/importmap pin [module].
Step 2: Check other options
Options B, C, and D are incorrect: B uses an incorrect verb add, C uses invalid rails importmap pin, and D misses the bin/ prefix.
4. You run rails importmap pin my_module but your JavaScript module does not load in the browser. What is the most likely mistake?
medium
A. You forgot to add <%= javascript_importmap_tags %> in your layout
B. You used pin instead of add command
C. You need to restart the Rails server after pinning
D. You must compile assets with webpack first
Solution
Step 1: Check how import maps load modules
Pinning registers the module, but javascript_importmap_tags must be in the layout to load scripts.
Step 2: Review other options
You used pin instead of add command is incorrect because pin is 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 A
Quick Check:
Missing javascript_importmap_tags = no JS load [OK]
Hint: Add javascript_importmap_tags in layout to load modules [OK]
Common Mistakes:
Not including javascript_importmap_tags in views
Confusing pin with add command
Thinking server restart is required
Assuming webpack is needed with import maps
5. You want to use a third-party JavaScript library hosted on a CDN with import maps in Rails 7+. Which is the correct way to pin it in config/importmap.rb?
hard
A. pin 'library', from: 'https://cdn.example.com/library.js'
B. pin 'library', to: '/assets/library.js'
C. pin 'library', to: 'https://cdn.example.com/library.js'
D. pin 'library', url: 'https://cdn.example.com/library.js'
Solution
Step 1: Understand pin syntax for external URLs
To pin a module from a CDN, use pin 'name', to: 'URL' in config/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 keys from and url which are invalid.
Final Answer:
pin 'library', to: 'https://cdn.example.com/library.js' -> Option C
Quick Check:
Pin CDN module = pin 'name', to: 'URL' [OK]
Hint: Use 'to:' with CDN URL when pinning external modules [OK]
Common Mistakes:
Using wrong keys like 'from' or 'url'
Pinning local path instead of CDN URL
Confusing syntax for pinning external scripts
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.