futura PRO
Joined 5/9/2020
futura PRO said over 2 years ago on Hotwire Turbo Replacing Rails UJS :
Another option that mimics the HTML response is to send the same format.html redirect within the turbo_stream block, along with a status of "303 See other". (Of course, you'll give up the magic of removing the DOM element in place and you'll reload the entire page.)

Here are the relevant threads:

https://github.com/hotwired/turbo/issues/84
https://github.com/hotwired/turbo/issues/320

So, just copy the format.html and add status: 303, which will remove the DELETE verb from the redirect ...

  respond_to do |format|
    format.html { redirect_to products_url, notice: "Product was successfully destroyed." }
    format.json { head :no_content }
    format.turbo_stream { redirect_to products_url, notice: "Product was successfully destroyed.", status: 303 }
  end


futura PRO said over 2 years ago on Hotwire Turbo Replacing Rails UJS :
This is a small thing, but ... if you always want the destroy link to perform the #confirm action every time (and I do), you can instead set the action in the Stimulus controller  with:

// confirmation_controller.js
  connect() {
    this.element.dataset.action = "confirmation#confirm"
  }

That allows us to drop the one line from the link in views/products/index.html.erb, so the data looks like:
data: {
        "turbo-method": :delete,
        controller: "confirmation",
        "confirmation-message-value": 'Are you sure?'
      }


futura PRO said over 2 years ago on Materialized Views in PostgreSQL :
Thanks for this one! Way back in the day, pre-Rails, I used mySQL views to simplify queries in my PHP apps, and they were really handy. I was disappointed that Rails didn't seem to make room for them. I wasn't aware of this gem, but it seems well done. Especially nice that it can use materialized views for the performance benefit.

futura PRO said 7 months ago on Unsaved Changes :
I'm sure there are JS junkies who can talk in more depth, but here's the way I think about it.

When, for example, this.setChanges() is run, you want the "this" in the line:

this.hasChanges = true

... to refer to the Stimulus controller.

But if you do addEventListener(), the object that triggers the function becomes the "this" inside the function. So when window.addEventListener()  is triggered, "this" refers to window. For this.element.addEventListener(), "this.element" is the "this." So when the event is triggered, this.element.addEventListener() ... runs the equivalent of:

this.element.hasChanges = true

... unless you bind it to the controller first.

But I do the same thing! I forget to bind and it's not working, I always just think, "Oh, I guess I need to bind."