Jorge Ortíz
Jorge Ortíz

Reputation: 1

form_with and stimulus with hotwire doesn't work (Rails 7)

Select field with form_with, Stimulus and Hotwire, and Rails 7

I want to see a message in the console once I change the select value. I'm using Hotwire and Rails 7 but it doesn't work.

Code.

<%= form_with model: [@framework, @framework.criteria.build] do |form| %>
  <div>
    <%= form.label :criteria_name %>
    <%= form.text_field :criteria_name, class: "rounded-md border border-pbgray-medium w-full mb-2" %>
  </div>

  <div data-controller="answer">
    <div>
      <%= form.label :type_answer %>
      <%= form.select :type_answer, [["Multiple Choice", 1], ["Single choice", 2], ["Text-box", 3]], {data: { action: "change->answer#my_method"}} %>
    </div>
  </div>

    <p data-action="click->answer#my_method">Click me!!</p>
  <div>
    <%= form.submit %>
  </div>
<% end %>

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  my_method() {
    console.log("Hello new World");
  }
}

Upvotes: 0

Views: 1155

Answers (1)

Jorge Ort&#237;z
Jorge Ort&#237;z

Reputation: 1

Nevermind, I found the issue:

Solution.

<%= form.select(:type_answer, [["Multiple choice", 1], ["Text-box", 2]], {}, { :class => 'some-class', :data => {:action => "click->answer#my_method"} }) %>

Upvotes: 0

Related Questions