我真的很难让 Stimulus 控制器在我正在开发的 Rails 7 应用程序中运行,并且非常感谢任何人可能提供的帮助。我一直在旋转我的轮子。
我的应用程序.js
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@hotwired/turbo-rails";
import "controllers";
import 'bootstrap';
我将刺激固定在 importmap.rb 中,如下所示:
pin "application", preload: true
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin "jquery", to: "https://cdn.jsdelivr.net/npm/[email protected] /cdn-cgi/l/email-protection/dist/jquery.js"
pin_all_from "app/javascript/controllers", under: "controllers"
我还没有触及 javascript/controllers/application.js 或 index.js 文件。
我的刺激控制器(ingredients-controller.js):
import { Controller } from '@hotwired/stimulus';
export default class extends Controller {
connect () {
console.log('HELLOOO!!!!');
}
addIngredients(event) {
event.preventDefault();
alert('test');
}
}
连接的<div>
在我看来下面。我现在所做的就是得到<button>
元素来 PreventDefault() 并执行测试警报。我无法从上面的刺激控制器得到任何响应。
<div data-controller="ingredients">
<turbo-frame id=<%= f.field_id(:ingredents) %>>
<h2>Add Ingredients</h2>
<%# Selection section %>
<div>
<h6>Spirits</h6>
<%= f.collection_select :spirit_id, Spirit.all, :id, :spirit_type, {}, { :size => "5", :multiple => true } %>
<h6>Mixers</h6>
<%= f.collection_select :mixer_id, Mixer.all, :id, :mixer_type, {}, { :size => "5", :multiple => true } %>
<h6>Garnishes</h6>
<%= f.collection_select :garnish_id, Garnish.all, :id, :garnish_type, {}, { :size => "5", :multiple => true } %>
</div>
<%# Selected Ingredients %>
</turbo-frame>
<button data-action="click->ingredients#addIngredients">Add Ingredients</button>
</div>
如果有人知道我在这里缺少什么,我将不胜感激!谢谢你!