0

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");
  }
}

1 Answers1

0

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"} }) %>

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 23 '22 at 09:36