1

如果有人能展示在 rails 6 或 rails 7 Alpha 2 引擎中使用 jquery ui 所需的确切步骤,我将不胜感激。我无法让 importmap-rails 在 rails 7 引擎中工作,也无法让 webpacker 在 Rails 6 引擎或 rails 7 alpha 2 引擎中工作。给定一个名为 custom_page 的引擎,使用生成

rails plugin new custom_page --mountable --full

我将 jquery-ui-rails 作为依赖项包含在 gemspec 中。

spec.add_dependency 'jquery-ui-rails'

也许这应该是 runtime_dependency?依赖项的完整列表是

  spec.add_dependency "rails", "~> 7.0.0.alpha2"

  spec.add_dependency 'new_ckeditor'

  spec.add_dependency 'ancestry'
  spec.add_dependency 'friendly_id', '>= 5.4.0'
  spec.add_dependency 'pg_search'

  spec.add_dependency 'carrierwave', '~> 2.0'
  spec.add_dependency 'carrierwave-imageoptimizer'
  spec.add_dependency 'sassc-rails', '~> 2.0.0'
  spec.add_dependency 'pg', '~> 1.1'
  spec.add_dependency 'jquery-rails'
  spec.add_dependency 'jquery-ui-rails'

  spec.add_development_dependency "puma"

  #Testing Gems
  spec.add_development_dependency "rspec-rails", '>= 5.0'
  spec.add_development_dependency "factory_bot_rails"
  spec.add_development_dependency "guard-rspec"

  spec.add_development_dependency 'capybara', '>= 3.32'
  spec.add_development_dependency 'selenium-webdriver'
  spec.add_development_dependency 'launchy'
  spec.add_development_dependency 'database_cleaner-active_record'

我在engine.rb中也需要同样的东西

require 'jquery-ui-rails'
require 'friendly_id'
require 'ancestry'

    module CustomPage
      class Engine < ::Rails::Engine
        isolate_namespace CustomPage
    
        config.generators do |g|
          g.test_framework :rspec,
            fixtures: false,
            request: false,
            view_specs: false,
            helper_specs: false,
            controller_specs: false,
            routing_specs: false
          g.fixture_replacement :factory_bot
          g.factory_bot dir: 'spec/factories'
        end
      end
    end

我在视图中添加了一个简单的测试

<p id="notice"><%= notice %></p>

<script type='text/javascript'>
  $(function() {
    $('.datepicker').datepicker();
  });
</script>

我已将要求包含在app/assets/stylesheets/custom_page/application.css

/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
 * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
 *
 * You're free to add application-wide styles to this file and they'll appear at the bottom of the
 * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
 * files in this directory. Styles in this file should be added after the last require_* statement.
 * It is generally better to create a new file per style scope.
 *
 *= require_tree .
 *= require_self
 */
 /*
  *= require jquery-ui
 */

而且在 app/assets/config/custom_page_manifest.js

//= link_directory ../stylesheets/custom_page .css
//= require jquery-ui

在 Firefox 显示中检查浏览器控制台时出现错误

Uncaught ReferenceError: $ is not defined

我显然在这里展示了 Rails 7 alpha 2 示例,但是使用 rails 6.1.4 也会出现同样的问题

本练习的目的是让我能够使用 jquery 库 jqtree,如果我能够使用 importmap-rails,那么设置将很简单,但是,根据我的未回答问题我无法这样做。

所以我真的在问如何在 Rails 6.1.4 引擎或 Rails 7 alpha 2 库中使用 jquery 库

4

0 回答 0