Bird
Raised Fist0
Ruby on Railsframework~20 mins

Stimulus and Turbo (Hotwire) in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Hotwire Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
Stimulus Controller: What happens on button click?

Given this Stimulus controller, what will be the console output when the button is clicked?

export default class extends Controller {
  static targets = ["output"]

  connect() {
    this.outputTarget.textContent = "Connected";
  }

  greet() {
    console.log("Hello from Stimulus!");
    this.outputTarget.textContent = "Greeted";
  }
}

And the HTML:

<div data-controller="hello">
  <button data-action="click->hello#greet">Click me</button>
  <span data-hello-target="output"></span>
</div>
AConsole logs nothing and the span text remains empty
BConsole logs "Connected" and the span text changes to "Hello from Stimulus!"
CConsole logs "Greeted" and the span text changes to "Connected"
DConsole logs "Hello from Stimulus!" and the span text changes to "Greeted"
Attempts:
2 left
💡 Hint

Remember the connect() method runs when the controller loads, and the greet() method runs on button click.

📝 Syntax
intermediate
1:30remaining
Turbo Frame: Correct attribute to load content asynchronously?

Which attribute should you add to a <turbo-frame> tag to load its content asynchronously when the frame is first shown?

Aloading="lazy"
Bdata-turbo-action="lazy-load"
Casync="true"
Dsrc="url"
Attempts:
2 left
💡 Hint

Think about how browsers handle lazy loading for images and frames.

❓ state_output
advanced
2:00remaining
Stimulus: What is the value of count after 3 clicks?

Consider this Stimulus controller:

export default class extends Controller {
  static values = { count: Number }

  connect() {
    this.countValue = 0;
  }

  increment() {
    this.countValue++;
    console.log(this.countValue);
  }
}

And the HTML:

<div data-controller="counter">
  <button data-action="click->counter#increment">Add</button>
</div>

What will be logged after clicking the button 3 times?

A0, 1, 2
B1, 2, 3
C3, 3, 3
Dundefined, undefined, undefined
Attempts:
2 left
💡 Hint

Check how countValue is initialized and updated.

🔧 Debug
advanced
2:30remaining
Turbo Stream: Why does the update not appear on the page?

You have a Turbo Stream response to update a <div id="message"> but the page does not update. What is the most likely cause?

Turbo Stream response:
<turbo-stream action="update" target="message">
  <template>New message content</template>
</turbo-stream>
HTML snippet:
<div id="msg">Old message</div>
AThe target id in the Turbo Stream does not match the HTML element's id
BThe Turbo Stream action "update" is invalid
CThe template tag is missing a closing tag
DTurbo Streams require the target to be a class, not an id
Attempts:
2 left
💡 Hint

Check the target attribute and the HTML element's id carefully.

🧠 Conceptual
expert
3:00remaining
How does Turbo Drive improve user experience in Rails apps?

Which statement best explains how Turbo Drive enhances navigation in Rails applications?

AIt disables JavaScript to improve page load speed and reduce errors
BIt caches all pages on the client to allow offline access without server requests
CIt replaces full page reloads with partial page updates by intercepting link clicks and form submissions
DIt automatically converts all Rails views into React components for faster rendering
Attempts:
2 left
💡 Hint

Think about how Turbo Drive handles navigation events.

Practice

(1/5)
1. What is the main purpose of Stimulus in a Rails application using Hotwire?
easy
A. To add simple JavaScript behavior to HTML elements using controllers and data attributes
B. To replace the entire page content on every user action
C. To handle database queries efficiently
D. To style HTML elements with CSS

Solution

  1. Step 1: Understand Stimulus role

    Stimulus is designed to add JavaScript behavior to HTML by connecting controllers to elements via data attributes.
  2. Step 2: Compare other options

    Replacing entire page content is Turbo's job, not Stimulus. Database queries and styling are unrelated.
  3. Final Answer:

    To add simple JavaScript behavior to HTML elements using controllers and data attributes -> Option A
  4. Quick Check:

    Stimulus = JavaScript behavior via controllers [OK]
Hint: Stimulus controls behavior, Turbo controls page updates [OK]
Common Mistakes:
  • Confusing Turbo's page update role with Stimulus
  • Thinking Stimulus handles styling or database
  • Assuming Stimulus reloads full pages
2. Which of the following is the correct way to connect a Stimulus controller named hello to a button element?
easy
A. Click me
B. Click me
C. Click me
D. Click me

Solution

  1. Step 1: Recall Stimulus controller connection syntax

    Stimulus controllers are connected using the data-controller attribute with the controller name.
  2. Step 2: Verify other attributes

    data-action defines event handlers, data-target defines targets inside controllers, data-connect is invalid.
  3. Final Answer:

    <button data-controller="hello">Click me</button> -> Option B
  4. Quick Check:

    Controller connection uses data-controller [OK]
Hint: Use data-controller to link Stimulus controllers [OK]
Common Mistakes:
  • Using data-action instead of data-controller to connect controller
  • Confusing data-target with controller connection
  • Using non-existent data-connect attribute
3. Given this Stimulus controller code:
export default class extends Controller {
  static targets = ["output"]
  greet() {
    this.outputTarget.textContent = "Hello, Stimulus!"
  }
}
and this HTML:
<div data-controller="greeting">
  <button data-action="click->greeting#greet">Say Hello</button>
  <span data-greeting-target="output"></span>
</div>
What happens when the button is clicked?
medium
A. The span text changes to 'Hello, Stimulus!'
B. Nothing happens because the controller name is wrong
C. The page reloads
D. An error is thrown because outputTarget is undefined

Solution

  1. Step 1: Match controller and targets

    The controller is named 'greeting' and defines an output target. The HTML uses data-controller="greeting" and data-greeting-target="output" correctly.
  2. Step 2: Understand the greet method and action

    The button triggers greet on click, which sets the outputTarget's textContent to 'Hello, Stimulus!'.
  3. Final Answer:

    The span text changes to 'Hello, Stimulus!' -> Option A
  4. Quick Check:

    Correct controller and target update text [OK]
Hint: Check controller name matches data-controller and targets [OK]
Common Mistakes:
  • Mismatching controller name and data-controller attribute
  • Forgetting to define static targets array
  • Expecting page reload instead of DOM update
4. Identify the error in this Turbo Frame usage:
<turbo-frame id="messages">
  <%= render @messages %>
</turbo-frame>
When a new message is created, the frame does not update automatically. What is the likely cause?
medium
A. The frame id should be messages_frame instead of messages
B. Turbo Frames cannot render collections
C. The render call is missing a partial
D. The server response does not include a turbo-frame with matching id

Solution

  1. Step 1: Understand Turbo Frame update mechanism

    Turbo Frames update only if the server response includes a turbo-frame element with the same id as the frame on the page.
  2. Step 2: Identify the problem

    If the server response lacks a matching turbo-frame wrapper, Turbo cannot replace the frame content automatically.
  3. Final Answer:

    The server response does not include a <turbo-frame> with matching id -> Option D
  4. Quick Check:

    Matching turbo-frame id in response is required [OK]
Hint: Ensure server response wraps content in matching turbo-frame id [OK]
Common Mistakes:
  • Changing frame id arbitrarily
  • Assuming Turbo Frames can't render collections
  • Forgetting to wrap response in turbo-frame tag
5. You want to create a live search feature using Turbo and Stimulus. Which approach correctly combines both to update search results as the user types without full page reload?
hard
A. Use Stimulus to reload the whole page manually on input change
B. Use Turbo Frames alone to reload the entire page on every keystroke
C. Use a Stimulus controller to listen for input events, send fetch requests to the server, and update a Turbo Frame with the results
D. Use Turbo Streams to replace the entire page content on input

Solution

  1. Step 1: Understand live search requirements

    Live search updates results dynamically as the user types without full reload.
  2. Step 2: Combine Stimulus and Turbo correctly

    Stimulus listens to input events and sends fetch requests. Turbo Frame updates only the results part of the page.
  3. Step 3: Evaluate other options

    Reloading entire page on every keystroke is inefficient. Turbo Streams replace parts but need server push. Stimulus alone can't reload page efficiently.
  4. Final Answer:

    Use a Stimulus controller to listen for input events, send fetch requests to the server, and update a Turbo Frame with the results -> Option C
  5. Quick Check:

    Stimulus + Turbo Frame = dynamic partial updates [OK]
Hint: Stimulus handles events, Turbo Frame updates partial content [OK]
Common Mistakes:
  • Reloading full page on every input
  • Using Turbo Frames without Stimulus for input events
  • Misusing Turbo Streams for full page replacement
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard