All threads / Stimulus JS Twitter UI: Part 1 Discussion

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Stimulus JS Twitter UI: Part 1 Discussion

Is the source code for this (and other) episodes available? I am having some small problems following along.
Reply
Great episode! I did find some things didn't reproduce with Rails 5.2 etc.
Reply

Still pretty new to rails. What would happen during create if the @tweet.save failed validation? Trying to understand how you'd use create.js.erb to display the form with errors in create if it fails?

Reply

Great video Chris.

From Stimulus' docs, I found that we can refactor the controller class using javascript getter and setter methods, like so:

import { Controller } from "stimulus"

export default class extends Controller {
  static targets = [ "body", "characterCount" ]

  initialize() {
    this.characterCount = this.body.length
  }

  update() {
    console.log(`updating...${this.body.length}`)
    this.characterCount = this.body.length
  }

  get body() {
    return this.bodyTarget.value
  }

  set characterCount(value) {
    this.characterCountTarget.textContent = value
  }
}
Reply

I am having an issue deploying a site with stimulus via capistrano. I get the error:

ModuleNotFoundError: Module not found: Error: Can't resolve './controllers' in '/home/deploy/site/releases/20190501124321/app/javascript/packs'

My directory is:

App/javascript/packs/controllers. Inside packs I have application.js which references ./controllers.

Any ideas why this is failing?

UPDATE: I figured it out. My controllers folder didn't have anything in it yet and was not syncing.

Reply

Hi Chris,

great video!

One thing I like to do these days is, instead of responding with a create.js.erb, catch the ajax:success event in the stimulus controller (possibly even by attaching it to document: https://stimulusjs.org/reference/actions#global-events).

You don't get inline rendering of your Rails ivar this way, but you stay in the context of your controller and don't have to use that kind of "hacky" way of updating the form/character count, since you have access to this.element and your targets...

Reply
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 38,558+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.