如何在 Rails 6 或 Rails 7 alpha 引擎中使用 jqueryUI

2024-04-17

如果有人能够展示在 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,那么设置起来会很简单,但是,根据我未解决的、尚未得到解答的问题here https://stackoverflow.com/questions/69635552/includemap-rails-in-rails-7-alpha-2-is-this-possible,我无能为力。

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


这并不像我想象的那么简单。首先,jquery-ui-rails and jquery-rails看起来已经过时了。我认为你不应该使用这些宝石。我们可以使用导入映射,因为我们已经设置它 https://stackoverflow.com/questions/69635552/how-to-set-up-importmap-rails-in-rails-7-engine已经为发动机了。但用途并不限于发动机。

# 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/[email protected] /cdn-cgi/l/email-protection/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:[email protected] /cdn-cgi/l/email-protection/jquery-ui.js"

# works fine
pin "jqtree", to: "https://ga.jspm.io/npm:[email protected] /cdn-cgi/l/email-protection/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,dynamicimport()可用于加载 jquery 插件。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#dynamic_imports 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:[email protected] /cdn-cgi/l/email-protection/dist/jquery.js"
pin "jquery-ui-dist", to: "https://ga.jspm.io/npm:[email protected] /cdn-cgi/l/email-protection/jquery-ui.js"
pin "jqtree", to: "https://ga.jspm.io/npm:[email protected] /cdn-cgi/l/email-protection/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;

对于导轨 6 设置jquery-ui-rails and jquery-rails宝石。确保您也需要 jquery。不要碰清单.js,你不是预编译jquery,而是预编译应用程序。{css,js}其中已经包含 jquery。你必须添加require to 应用程序.js以及。

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

https://github.com/jquery-ui-rails/jquery-ui-rails#require-everything 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 */
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Rails 6 或 Rails 7 alpha 引擎中使用 jqueryUI 的相关文章

随机推荐

  • 有没有办法将色调仅应用于seaborn中PairGrid的下部

    我想知道是否可以将色调仅应用于seaborn的下部PairGrid For example say I have the following figure 对于我需要展示的内容 我想将密度图保留在对角线上 将整体散点图保留在上部 在其上方打
  • 连接 Apollo 和 mongodb

    我想将我的 Apollo 服务器与我的 mongoDB 连接 我知道那里有很多例子 但我陷入了异步部分 没有找到解决方案或示例 这很奇怪 我完全错了吗 我从 next js 的示例开始https github com zeit next j
  • “窗口”类型不支持直接内容

    我有一个使用 VS 2015 构建的 WPF 解决方案 由多个项目组成 突然 我开始在设计模式下收到一条警告 内容如下 窗口 类型不支持直接内容 我了解某些控件不支持直接内容 但是System Windows Window应该 我收到同样的
  • Android 编程错误

    我正在使用 Eclipse Galileo 编写 Android 你好 测试 教程 http developer android com resources tutorials testing helloandroid test html
  • ASP.net 页面在导入语句上出现错误,但我确实有引用吗?

    任何想法为什么我在我的 MVC2 项目中收到以下错误 即使在项目本身中我肯定有对 system Web Entity 的引用 Compiler Error Message CS0234 The type or namespace name
  • 后台根据时间激活本地通知

    因此 我有一个包含重复间隔本地通知的应用程序 我想添加一个在睡眠期间暂停通知的功能 到目前为止 我已经为用户创建了两个日期选择器 以指定他们想要停止重复间隔的时间以及自动重新启动的时间 我还为他们添加了一个 uiswitch 来激活睡眠模式
  • 在 PHP 中将 Oauth 2.0 访问令牌传递给 Fusion Tables API 时出现无效凭据错误

    我已经达到了沮丧的地步 正在寻求帮助 我整个周末都在学习新东西 以便尝试弄清楚如何使用需要通过 Oauth 2 0 进行身份验证的 goolge fusion table API 我开始使用 php 进行开发只是因为我能够找到一些帮助我走上
  • 将事件处理程序应用于动态控制

    我有一个用户窗体 可以动态放置commandButton到用户表单上 但我似乎无法正确设置动态事件处理程序 下面显示了我如何设置动态按钮的代码 Set cButton Me Controls Add Forms CommandButton
  • 使 fetch 调用真正同步

    是的 我想完全同步 我知道它会完全停止我唯一的线程 但我真的需要它 因为我使用一些我不想更改的 SDK 并且在这个 SDK 中 您需要传递一个将被调用且会更改的函数那里有一些价值 比如 function onNonce stuff cons
  • 如何使用 Sql Server 2008 删除表中的前 1000 行?

    我在 SQL Server 中有一个表 我想从中删除前 1000 行 但是 我尝试了此操作 但我不是只删除前 1000 行 而是删除了表中的所有行 这是代码 delete from mytab select top 1000 a1 a2 a
  • 如何在AWS EC2服务器中编写cron作业

    我在 AWS EC2 中创建了一个 cron 作业 但它不起作用 我按照以下步骤创建 crontab 第1步 我登录到AWS EC2实例 step 2 crontab e 第三步 插入模式 第4步 我输入了 php var www html
  • 处理多种表单和打印内容[关闭]

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 help reopen questions 您好 我对
  • 从 JBPM WorkItemHandler 抛出异常?

    我对从 JBPM 工作项处理程序抛出异常以及在业务流程中的其他地方处理异常的主题有点困惑 我们使用 JBPM 6 0 3 在 Jboss EAP 6 1 中运行 The JBPM 用户指南 http docs jboss org jbpm
  • ExtJS TreeGrid 中的复选框列

    有没有办法在新的 extjs 小部件 TreeGrid 中包含复选框列 将节点属性标记为 false true 并不像 TreePanel 那样有效 Cheers 我修改了 Ext ux tree TreeGridNodeUI 类来实现此功
  • 正则表达式捕获 VBA 注释

    我正在尝试捕获 VBA 注释 到目前为止我有以下内容 Z 它捕获以单引号开头但在字符串末尾之前不包含任何双引号的任何内容 即它不会匹配双引号字符串中的单引号 dim s as string a string variable works s
  • 未知层:当我尝试加载模型时 KerasLayer

    当我尝试将模型另存为 hdf5 时 path path h5 model save path 然后再次加载模型 my reloaded model tf keras models load model path 我收到以下错误 ValueE
  • 运行为黑莓设备创建的黑莓应用程序需要哪些步骤?

    我使用 java me 和 BlackBerry 特定 API 创建了一个 BlackBerry 应用程序 它在黑莓模拟器上运行良好 我想知道如何将此应用程序部署到 BlackBerry 设备 从文档中我发现 在设备上运行 BlackBer
  • XSLTProcessor::importStylesheet() 中的多个 PHP 警告

    Errors Warning XSLTProcessor importStylesheet xsltprocessor importstylesheet Undefined variable in transform php on line
  • 在 Cloud Dataflow 中进行 ETL 和解析 CSV 文件

    我是云数据流和 Java 的新手 所以我希望这是正确的问题 我有一个 csv 文件 其中有 n 个列和行 可以是字符串 整数或时间戳 我需要为每一列创建一个新的 PCollection 吗 我在示例中找到的大多数文档都类似于 PCollec
  • 如何在 Rails 6 或 Rails 7 alpha 引擎中使用 jqueryUI

    如果有人能够展示在 Rails 6 或 Rails 7 Alpha 2 引擎中使用 jquery ui 所需的确切步骤 我将不胜感激 我无法让 importmap rails 在 Rails 7 引擎中工作 也无法让 webpacker 在