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.html.erb
<%= 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 %>
- answer_controller.js
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
my_method() {
console.log("Hello new World");
}
}