【问题标题】:Stimulus Controllers Not Functioning At All In Rails 7 AppStimulus 控制器在 Rails 7 应用程序中根本不起作用
【发布时间】:2023-01-31 21:53:55
【问题描述】:

我真的很努力让 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';

我将 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 文件。

我的刺激控制器 (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>

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

【问题讨论】:

    标签: javascript ruby ruby-on-rails-7 stimulusjs import-maps


    【解决方案1】:

    我有一个类似的问题(尽管使用 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. 测试您的 Stimulus 控制器

      希望能帮助到你!

    【讨论】:

    • 谢谢戈加马尔!这奏效了。我遵循了这些步骤,并且还重新排列了 application.js 中的导入语句,以便导入“jquery”位于顶部。它以前在底部,在我上面的帖子中被截断了。不确定这是否也导致了问题,但现在一切正常。
    【解决方案2】:

    就我而言,我必须手动编辑我的app/javascript/controllers/index.js 来注册控制器。

    例如,在 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)
    

    【讨论】:

      猜你喜欢
      • 2022-08-15
      • 2013-01-06
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      相关资源
      最近更新 更多