Nicolás Kruk

Joined

11,350 Experience
106 Lessons Completed
1 Question Solved

Activity

Found the issue. The options passed should be an empty object. Nil will return an error so finally:

<%= form.collection_select :product_type, Product.product_types.keys, :to_s, :titlecase, {}, data: { action: "change->forms#handleChange" } %>

I found this issue on SO which pretty much is my issue:
https://stackoverflow.com/questions/63964037/cant-make-stimulus-js-data-action-in-collection-select-rails-6

But I can't make that solution work.

In my _form partial I have something like:

<div data-controller="forms">  
    <div class="form-group">
      <%= form.label :product_type %>
      <%= form.collection_select :product_type, Product.product_types.keys, :to_s, :titlecase, data: {action: "change->forms#handleChange"} %>
    </div>
    ...
    </div>
  </div>

And in my Forms Stimulus Controller I have:

export default class extends Controller {
  initialize() {
    console.log("Stimulus at your service!")
  }

  connect() {
    console.log("connected to forms controller");
  }

  handleChange() {
    console.log("the dropdown changed");
  }
}

The initiliaze and connect logs work fine. I get nothing when I change the values of the dropdown though.

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

© 2024 GoRails, LLC. All rights reserved.