rails 自定义主键

Ihave recently had the pleasure of updating our app from Rails 4 to Rails 6. Though it initially seemed to be a very smooth transition, I came across some minor problems as testing got underway.

最近, 我很高兴将我们的应用程序从Rails 4升级到Rails6。尽管最初的过渡似乎很顺利,但是随着测试的进行,我遇到了一些小问题。

The biggest issue was, of course, that NONE of my custom Javascript modules worked and I was encountering bugs with my Bootstrap and SB-Admin-2 JS scripts.

当然,最大的问题是我的自定义Javascript模块都无法正常工作,而且我的Bootstrap和SB-Admin-2 JS脚本遇到了错误。

Some of the bugs I was seeing in my browser developer console were:

我在浏览器开发人员控制台中看到的一些错误是:

TypeError: $(…).metisMenu is not a function

TypeError:$(…).metisMenu不是函数

Uncaught ReferenceError: $ is not defined

未捕获的ReferenceError:未定义$

And absolutely nothing online was (a) relevant to my stack and (b) solving my problem. So for this reason, I thought I’d put up something in case other people run into the same problem.

绝对没有任何在线内容(a)与我的筹码有关以及(b)解决我的问题。 因此,出于这个原因,我想我会提出一些建议,以防其他人遇到相同的问题。

NB: Other solutions online may work for those who do not implement Turbolinks in their stack.

注意:其他在线解决方案可能适用于未在其堆栈中实现Turbolink的用户。

Webpacker设置 (Webpacker Set-Up)

As this guide is not for beginners, I will not be giving an introduction to Webpacker (if needed, see: Guide 1, Guide 2 and Guide 3). I found the directory structure below most suitable for my needs:

由于本指南不适合初学者使用,因此我将不对Webpacker进行介绍(如果需要,请参阅: 指南1 , 指南 2和指南3 )。 我发现以下最适合我的目录结构:

When you run rails webpack:install, the javascript folder is automatically created, complete with the packs folder and the application.js and application.scss files. Taking this structure, I added another folder, custom, which I used to store my custom and third-party modules.

运行rails webpack:install ,将自动创建javascript文件夹,其中包含packs文件夹以及application.jsapplication.scss文件。 采用这种结构,我添加了另一个文件夹custom ,该文件夹用于存储自定义模块和第三方模块。

My application.scss file is empty for the moment and the contents of my application.js are:

我的application.scss文件目前为空,而application.js的内容为:

require(“@rails/ujs”).start()require(“turbolinks”).start()require(“@rails/activestorage”).start()require(“channels”)require(“jquery”)require(‘popper.js’)require(“jquery.easing/jquery.easing”)require(‘@fortawesome/fontawesome-free/js/all’)require(“bootstrap/dist/js/bootstrap.bundle”)require(“channels”)require(“./custom/modal”)require(“./custom/sb-admin-2”)console.log(‘Hello from application.js’)

(Keep in mind that if you copy the above code directly, you will get errors as the quotation marks are not recognised!!!)

(请记住,如果直接复制上面的代码,由于引号不能被识别,将导致错误!!!)

My custom modal module pertains to the functionality of my modals. I wanted their contents to be changed depending on the link that was used to show them using data-target parameters. For reference I provided my full modal.js code at the bottom of the article.

我的自定义模式模块与我的模式的功能有关。 我希望根据使用数据目标参数向他们展示的链接来更改其内容。 作为参考,我在文章底部提供了完整的modal.js代码。

Below are the packages found in package.json, compare the list to yours and determine if you have any missing.

以下是在package.json中找到的软件包,将列表与您的列表进行比较,并确定是否缺少任何文件。

{  "dependencies": {    "@fortawesome/fontawesome-free": "^5.14.0",    "@rails/activestorage": "^6.0.3-2",    "@rails/ujs": "^6.0.3-2",    "@rails/webpacker": "5.1.1",    "autoclean": "^1.0.2",    "bootstrap": "^4.5.0",    "channels": "^0.0.4",    "core-js": "^3.6.5",    "expose-loader": "^1.0.0",    "fontawesome": "^5.6.3",    "jquery": "^3.5.1",    "jquery-ujs": "^1.2.2",    "metismenu": "^3.0.6",    "popper": "^1.0.1",    "popper.js": "^1.16.1",    "rails-ujs": "^5.2.4-3",    "regenerator-runtime": "^0.13.7",    "startbootstrap-sb-admin-2": "^4.1.1",    "turbolinks": "^5.2.0",    "yarn": "^1.22.4"  },  "devDependencies": {    "webpack-dev-server": "^3.11.0"  }}

And finally, in your app/config/webpack/environment.js, you want the following:

最后,在您的app / config / webpack / environment.js中 ,您需要以下内容:

const { environment } = require('@rails/webpacker');const webpack = require("webpack");environment.plugins.append("Provide", new webpack.ProvidePlugin({    $: 'jquery',    jquery: 'jquery',    jQuery: 'jquery',    'window.jQuery': 'jquery',    Popper: ['popper.js', 'default']}));module.exports = environment;

That’s all for the Webpacker. Moving on to the Javascript…

Webpacker就这些了。 继续使用Javascript ...

定制和第三方Javascript (Custom and Third Party Javascript)

The problem with running the custom modules alongside Turbolinks is that you are no longer waiting for the ‘document’ to load, but for Turbolinks to initialise the page. So while your code may have previously looked like this:

与Turbolinks一起运行自定义模块的问题在于您不再等待加载“文档”,而是等待Turbolinks初始化页面。 因此,虽然您的代码以前可能看起来像这样:

function readyFn(jQuery) {  console.log("Hello World!")};$(window).on('load', readyFn);

It will now have to look like this:

现在它将看起来像这样:

function readyFn(jQuery) {  console.log("Hello World!");};$(document).on('turbolinks:load', readyFn);// or $(document).on('turbolinks:load', function() {    function() {       console.log("Hello World!");   }};

I added the $(document).on('turbolinks:load', function() { //code } to the top of the SB-Admin-2.js file so that it was also comaptible with Turbolinks. And that’s it! I hope this was helpful and will save someone out there days of tearing their hair out and going for angry runs.

我将$(document).on('turbolinks:load', function() { //code }SB-Admin-2.js文件的顶部,以便它也可以与Turbolinks兼容。就是这样!我希望这会有所帮助,并且可以避免有人将头发撕裂并发怒的日子。

Modal.js (Modal.js)

$(document).on('turbolinks:load', function() {  $('#preview').on('shown.bs.modal',function (event) {    var link = $(event.relatedTarget);    var type = link.data('type');    var id = link.data('linkid');        var modal = $(this);    if (type.includes("video")) {      modal.find('.modal-body video').attr('src', id);      modal.find('.modal-body video').attr('id', 'modal-activated');      modal.find('.modal-body video').attr('data-type', type);      modal.find('.modal-body video').toggleClass('d-none');}    else if (type.includes("image")) {      modal.find('.modal-body img').attr('src', id);      modal.find('.modal-body img').attr('id', 'modal-activated');      modal.find('.modal-body img').attr('data-type', type);      modal.find('.modal-body img').toggleClass('d-none');    }  });});$(document).on('turbolinks:load', function () {  $('#preview').on('hidden.bs.modal', function (event) {    var link = $('#modal-activated');    var type = link.data('type');    var modal = $(this);if (type.includes("video")) {      modal.find('.modal-body video').toggleClass('d-none');    }    else if (type.includes("image")) {      modal.find('.modal-body img').toggleClass('d-none');    }  });});

翻译自: https://medium.com/@ekaterinalait_15121/a-guide-to-custom-and-third-party-javascript-with-rails-6-webpacker-and-turbolinks-9b36942b8789

rails 自定义主键


http://www.taodudu.cc/news/show-3726798.html

相关文章:

  • HydroCMS更换手风琴式侧栏metismenu
  • 插件学习:metisMenu.min.js
  • 读源码 | metisMenu侧边栏插件
  • MetisMenu : Jquery + CSS 实现可隐藏的二级侧边栏导航
  • java web动态菜单设计_spring-boot与模板引擎:使用metisMenu实现动态多级菜单
  • js-metisMenu
  • 使用Bootstrap+metisMenu完成简单的后台管理界面
  • metisMenu.js动态侧边导航的实现(ajax动态渲染部分导航)
  • metisMenu侧边栏插件
  • jquery.metisMenu.js插件
  • 软件生存周期过程GB_T8566-2007内容整理
  • 软件的生存周期
  • 信息技术软件生存周期过程
  • 软件生存周期阶段及任务简述
  • 软考—程序员(软件生存周期模型)
  • 为什么要有软件生存周期过程
  • 软考—程序员(软件生存周期)
  • 软件生存周期及其模型
  • 软件工程—绪论软件工程2(软件工程过程、软件生存周期、软件生存周期模型)
  • 软件生存周期及开发模型
  • 软件生存周期的过程,活动和任务
  • 软件生存周期及其模型是什么?
  • 软件生存周期与开发模型
  • [软件工程]软件生存周期过程与管理————(2020.6.29学习笔记)
  • 02_软件生存周期与软件过程
  • 软件生存周期过程及其模型
  • 软件工程-软件生存周期模型
  • 读书笔记:软件工程(3) - 软件生存周期
  • 软件生存周期模型
  • 软考——软件工程——软件生存周期

rails 自定义主键_带有Rails 6 Webpacker和turbolink的自定义和第三方javascript指南相关推荐

  1. MybatisPlus:SQL语句打印、SQL分析、自定义主键值策略填充(IdType.INPUT)、动态表名、多租户、枚举、类型处理器、连表自定义SQL(使用wrapper)

    文章目录 1. 简单使用以及配置 - 带分页配置 2. 用法 2.0 Wrapper属性 2.1 @TableId - 自定义主键生成策略 2.2 @TableField - 自定义字段值填充 2.3 ...

  2. MyBatis主键回填和自定义主键

    MyBatis主键回填和自定义主键 1. 主键回填 JDBC中的Statement对象在执行插入的SQL后,可以通过getGeneratedKeys方法获得数据库生成的主键,这样便能达到获取主键的功能 ...

  3. mongod自定义主键

    关于mongodb中设置主键问题 默认主键 ObjectId 类似唯一主键,可以很快的去生成和排序,包含 12 bytes,含义是: 前 4 个字节表示创建 unix 时间戳,格林尼治时间 UTC 时 ...

  4. rails 添加外键_如何在Rails后端中添加功能强大的搜索引擎

    rails 添加外键 by Domenico Angilletta 通过多梅尼科·安吉列塔(Domenico Angilletta) In my experience as a Ruby on Rai ...

  5. mybatis insert 返回主键_面试准备季——MyBatis 面试专题(含答案)

    话不多说,直接上题-- 1.什么是 Mybatis? (1)Mybatis 是一个半 ORM(对象关系映射)框架,它内部封装了 JDBC,开发时只需要关注 SQL 语句本身,不需要花费精力去处理加载驱 ...

  6. mapper mysql 主键_实现通用mapper主键策略兼容mysql和oracle

    [原创文章,转载请注明原文章地址,谢谢!] 1.直接用官方提供的注解方法是无法达到兼容效果的 2.跟踪源码看看是否有其他方法 3.这里有个genSql,可以看一下这个类 4.创建一个自定义的处理类实现 ...

  7. 可以给视图加复合主键_灵活视图处理的模式,第1部分–使用复合材料

    可以给视图加复合主键 I would dare say that views, in the many forms they can take, are underrated entities in ...

  8. pandas mysql主键_使用Autoincrement将Pandas Dataframe插入MySQL表自动生成主键

    我有一个Pandas数据帧,我正试图使用MySQLdb和to-sql将其插入到MySQL表中.该表的主键为'allocationid',并且为autoincrement..我希望每天都这样做,从MyS ...

  9. mysql 存储过程 主键_存储过程生成主键

    存储过程生成主键 MySQL delimiter $$CREATE PROCEDURE generateKeys(in pm_name varchar(20))begindeclare curr_Ke ...

最新文章

  1. NLP学习思维导图,非常的全面和清晰
  2. 开发最前沿:项目案例实战之桥模式
  3. jQuery验证用户名是否可用
  4. Boost:构造一个流对象,任何发送到此流将标准输出
  5. iOS 混合网页开发 问题
  6. 论文阅读(XiangBai——【CVPR2017】Detecting Oriented Text in Natural Images by Linking Segments)...
  7. python运维知识大全_python基础知识
  8. mac电脑安装mysql客户端
  9. loongson2f_龙芯灵珑9S2A一体机tftp+usb安装debian6 详细过程:
  10. CodeReview总结
  11. 初中计算机课程百科,理科、百科
  12. 互联网创业公司的经理​其实最主要是一个产品​经理
  13. 用尽可能多的字数介绍Leaky ReLU激活函数
  14. 如何制作一份疫情场所分布地图?(附数据和源码)
  15. Python爬虫 | 代理IP的获取和使用
  16. Linux strace工具
  17. GRE新东方推荐学习方法(2010年左右)
  18. HCIA-第八节0615
  19. 在线小说阅读网站开源项目地址整合
  20. 数据可视化大屏案例 总目录

热门文章

  1. Kruskal算法(最小生成树)
  2. Pycharm配置anaconda环境
  3. 视图操作 第3关:在视图中插入、修改、删除数据
  4. C/C++中的getenv()函数
  5. MindManager教你如何学好英语
  6. 21种唤醒灵性与财富的方法
  7. C语言求老师及其夫人的年龄
  8. 《网络安全与渗透测试》课堂笔记---06
  9. 数据挖掘基础-1.文本相似度
  10. Token认证的好处和坏处是什么?