繁体   English   中英

Stimulus 控制器在 Rails 7 应用程序中根本不起作用

[英]Stimulus Controllers Not Functioning At All In Rails 7 App

我真的很难在我正在开发的 Rails 7 应用程序中将 Stimulus 控制器设置为 function,我将不胜感激任何人可能提供的帮助。 我一直在旋转我的轮子。

我的应用程序.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';

我将 Stimulus 固定在 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/jquery@3.6.0/dist/jquery.js"
pin_all_from "app/javascript/controllers", under: "controllers"

我没有触及 javascript/controllers/application.js 或 index.js 文件。

我的刺激 Controller (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() 并进行测试警报。 我无法从上面的 Stimulus Controller 得到任何回应。

<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>

如果有人知道我在这里遗漏了什么,将不胜感激! 谢谢你!

我有一个类似的问题(尽管使用 JS 捆绑器),我通过手动安装 Stimulus 解决了它。 您可以在此处找到说明: https://github.com/hotwired/stimulus-rails

您可能需要先运行rails assets:clobber来反编译您已经编译的所有资产,在您完成 Stimulus 的手动安装之后,也许您应该在另一个终端选项卡中运行yarn build --watch (与运行 rails s 的方式相同) )

所以步骤:

  1. 运行rails assets:clobber

  2. stimulus-rails gem 添加到您的 Gemfile 中: gem 'stimulus-rails'

  3. 运行./bin/bundle install

  4. 按照https://github.com/hotwired/stimulus-rails中的手动安装说明(在子标题“With import map”下)进行操作。

  5. 在附加选项卡中运行yarn build --watch

  6. 测试你的刺激 controller

希望能帮助到你!

在我的例子中,我必须手动编辑我的app/javascript/controllers/index.js来注册 controller。

例如,在rails g component dropdown之后,我需要按如下方式编辑index.js (评论中的命令./bin/rails stimulus:manifest:update没有正确更新文件...)

// app/javascript/controllers/index.js
// This file is auto-generated by ./bin/rails stimulus:manifest:update
// Run that command whenever you add a new controller or create them with
// ./bin/rails generate stimulus controllerName

import { application } from "./application"

import DropdownController from "../../components/dropdown_component/dropdown_component_controller"
application.register("dropdown", DropdownController)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM