0
0
Ruby on Railsframework~20 mins

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

Choose your learning style9 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.