8

我有一个 Rails/webpacker/stimulus 项目。今天我将资产管道迁移到 webpacker。除了一个细节,一切都很好。

Link_to 按钮调用直接指向 show 操作的 destroy 方法。remote:true 标签也不起作用。ajax:beforeSend 动作不会被触发,确认框也不会被加载。

<%= link_to sanitize("<i class='fa fa-trash'></i>"), dashboard_manager_dashboard_path(dashboard), remote: true, method: :delete, data: {
                            confirm: "sure?",
                            action: 'ajax:beforeSend->dm--dashboard#onBeforeDelete ajax:success->dm--dashboard#onDelete'
                        } %>

我在 Stackoverflow 上发现了许多类似的案例,大多数都提到 Rails UJS 没有正确加载,这里似乎也是这种情况。但是,这些示例中的问题不使用 webpack。

Stackoverflow 上的一些帖子提到使用 button_to 而不是 link_to。但这不是我正在寻找的解决方案,我希望能够使用 remote:true 和 ajax 回调。

有人看到我的设置中的缺陷吗?在此先感谢您的任何建议!

应用程序.js:

import Rails from 'rails-ujs';
import Turbolinks from 'turbolinks';
import * as ActiveStorage from 'activestorage';

Rails.start();
Turbolinks.start();
ActiveStorage.start();

import "../src/import-jquery";
import 'bootstrap/dist/js/bootstrap';
import 'tempusdominus-bootstrap-4';
import '../src/bootstrap-select.js';
import '../src/custom_calender.js';
import '../src/functions.js';

import { Application } from "stimulus"
import { definitionsFromContext } from "stimulus/webpack-helpers"
const application = Application.start();
const context = require.context("controllers", true, /.js$/);
application.load(definitionsFromContext(context));

包.json:

{
    "name": "Youba",
    "private": true,
    "version": "1.0",
    "dependencies": {
        "@babel/core": "^7.4.4",
        "@rails/ujs": "^6.0.0-alpha",
        "@rails/webpacker": "^4.0.2",
        "activestorage": "^5.2.3",
        "babel-loader": "^8.0.6",
        "bootstrap": "^4.3.1",
        "bootstrap-select": "^1.13.10",
        "choices.js": "^7.0.0",
        "jquery": "^3.4.1",
        "jquery-ui": "^1.12.1",
        "jquery-ujs": "^1.2.2",
        "moment": "^2.24.0",
        "moment-timezone": "^0.5.11",
        "popper": "^1.0.1",
        "popper.js": "^1.14.7",
        "rails-ujs": "^5.2.3",
        "stimulus": "^1.1.1",
        "tempusdominus-bootstrap-4": "^5.1.2",
        "tempusdominus-core": "5.0.3",
        "turbolinks": "^5.2.0",
        "webpack": "^4.0.0",
        "webpack-cli": "^3.3.2"
    },
    "devDependencies": {
        "webpack-dev-server": "^3.2.1"
    }
}

环境.js

const {environment} = require('@rails/webpacker');

const webpack = require('webpack');
environment.plugins.append('Provide', new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery',
    jquery: 'jquery',
    Popper: ['popper.js', 'default']
}));

module.exports = environment;
4

2 回答 2

1

这可能为时已晚,但我在尝试学习 Rails 时遇到了类似的问题。我发现我没有
= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload'
app/assets/layouts/application.html.haml中包含该行

于 2020-03-17T18:47:58.590 回答
0

我在我的情况下遇到了同样的问题,这是因为我application.scssapp/javascript/pack/

如果我将 .scss 文件移动到样式表一切正常)

于 2020-09-03T20:59:23.627 回答