ruby-on-rails - 如何在 Rails 6 或 Rails 7 alpha 引擎中使用 jqueryUI

标签 ruby-on-rails jquery-ui ruby-on-rails-6 rails-engines ruby-on-rails-7

如果有人能展示在 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

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

<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,它的设置将很简单,但是,根据我公开的尚未回答的问题 here,我做不到。

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

最佳答案

这并不像我想象的那么简单。首先,jquery-ui-railsjquery-rails 似乎已经过时了。我认为你不应该使用那些 gem 。我们可以使用 importmaps 因为我们有 set it up已经为引擎。但用途不仅限于引擎。

# config/importmap.rb

# NOTE: pin jquery to jsdelivr. this will make jquery global on import;
#       jspm wraps packages in a module [1], so jquery is not available globally.
pin "jquery", to: "https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.js"

# NOTE: a few answers suggested `jquery-ui-dist`. I couldn't add it with 
#       `bin/importmap`; use https://generator.jspm.io/ to get the url.
pin "jquery-ui-dist", to: "https://ga.jspm.io/npm:jquery-ui-dist@1.13.1/jquery-ui.js"

# works fine
pin "jqtree", to: "https://ga.jspm.io/npm:jqtree@1.6.2/lib/tree.jquery.js"

# [1] someone, please, link to/explain what jspm does exactly to cause this.
// app/javascript/application.js

import "jquery";
import "jquery-ui-dist";
import "jqtree";

console.log(window.$);   // jQuery is already global
console.log($.ui);       // jquery-ui initialized on import
console.log($().tree);   // jqtree also initialized

如果出于某种原因你想坚持使用 jspm,可以使用动态 import() 加载 jquery 插件。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#dynamic_imports

# config/importmap.rb

pin "jquery", to: "https://ga.jspm.io/npm:jquery@3.6.0/dist/jquery.js"
pin "jquery-ui-dist", to: "https://ga.jspm.io/npm:jquery-ui-dist@1.13.1/jquery-ui.js"
pin "jqtree", to: "https://ga.jspm.io/npm:jqtree@1.6.2/lib/tree.jquery.js"
// app/javascript/application.js

import jQuery from "jquery";

// NOTE: keep in mind, these will be lazily loaded; use first method if this 
//       doesn't work in your set up.
import("jquery-ui-dist");
import("jqtree");

console.log(window.$);      // undefined
console.log(window.jQuery); // undefined
console.log(jQuery().ui);   // undefined
console.log(jQuery().tree); // undefined

// NOTE: make jquery global
window.$ = window.jQuery = jQuery;

对于带有 jquery-ui-railsjquery-rails gem 的 rails 6 设置。确保你也需要 jquery。不要触摸 manifest.js,您不是在预编译 jquery,您是在预编译已经包含 jquery 的 application.{css,js}。您还必须将 require 添加到 application.js

https://github.com/rails/jquery-rails#installation

https://github.com/jquery-ui-rails/jquery-ui-rails#require-everything

// app/assets/javascripts/application.js

//= require jquery
//= require jquery-ui
/* app/assets/stylesheets/application.css */

/*= require jquery-ui */

关于ruby-on-rails - 如何在 Rails 6 或 Rails 7 alpha 引擎中使用 jqueryUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69691718/

相关文章:

ruby-on-rails - 强制 Rails 的 app.get 重做请求

simple-form - Rails 6 操作文本 - 表单验证错误

ruby-on-rails - 使用 RSpec 3 在 block 之前运行一次

redis - Rails 6 - sidekiq 不会在开发中出队

ruby-on-rails - Rails - 使用标签或类型记录事件并获取电子邮件警报 - 最佳实践?

ruby-on-rails - 如何使用 Rails form_for 获取同一模型的多条记录

ruby-on-rails - 是否可以在本地文件夹中运行 Ruby 和 Rails,而无需先在 Mac OS X 上安装它们?

javascript - 将日期值(存储在变量中)分配给日期选择器

android - 我如何使用 Jquery Mobile 实现 MVC 架构

javascript - JQuery 在另一个模态对话框中打开一个模态对话框