给rails的ActiveModel::Errors类扩展了两个方法:insert、keep

用例见注释: #coding: utf-8 class ActiveModel::Errors # add的简写方法 # u.errors.append :code, '邮件验证码不正确' def append(*l) self.add l[0], :custom, message: l[1] end # 从开头插入,跟append相反 # u.errors.insert :code, '邮件验证码不正确' def insert(*l) errors = [[l[0], :custom, message: l[...
2022-12-02 23:39 | 阅读 824 次 | 评论 0 条

rails查看某个模型的所有验证规则

比如User模型,则用: User.validators
2022-10-28 20:28 | 阅读 562 次 | 评论 0 条

rails清空Sprockets缓存

在rails开发中,有时候修改了assets里的静态文件名不生效,可能是Sprockets缓存的问题,可以用下面的命令清空: rake tmp:cache:clear 然后再运行 rails s 即可,初次运行后刷新页面需要很长时间,因为缓存被清空了第一次要建立缓存,后面再刷新就很快了
2022-10-23 00:39 | 阅读 240 次 | 评论 0 条

opal对layer弹层的简单封装

def layer_open(h) type = h[:type] || 1 title = h[:title] skin = h[:skin] || 'layui-layer-rim' area = h[:are] || ['420px', '240px'] content = h[:content] success = h[:success] || proc {} layer_index = ` layer.open({ type: #{type}, title: #{title}, s...
2022-10-22 22:55 | 阅读 611 次 | 评论 0 条

rails使用has_secure_password进行身份验证

生成模型 rails g model User username:string email:string password_digest:string 在user模型加入has_secure_password class User < ApplicationRecord has_secure_password end
2022-10-19 23:43 | 阅读 661 次 | 评论 0 条

opal-jquery对等jquery里面的$(this)

evt.current_target对等$(this),如 Document.find('h1').on :click do |evt| puts evt.current_target.html end 对等jquery的 $('h1').click(function () { console.log($(this).html()) })
2022-10-19 11:59 | 阅读 205 次 | 评论 0 条

rails的数据库字段类型,migrations中常用到

The ActiveRecord data types available in Rails 5. :primary_key :string :text :integer :bigint :float :decimal :numeric :datetime :time :date :binary :boolean ...
2022-10-16 10:50 | 阅读 652 次 | 评论 0 条

ruby on rails 的 update、update_attribute、update_columns 的区别

触发Validation 触发Callbacks 改动updated_at update ✅ ✅ ✅ update_attribute ❌ ✅ ✅ ...
2022-10-14 01:18 | 阅读 267 次 | 评论 0 条

rails把db:migrate对数据库的变动应用到schema.rb

执行完 rake db:migrate 以后再执行 rake db:schema:dump
2022-09-24 15:36 | 阅读 839 次 | 评论 0 条

rails使用ruby3报错:Unable to load application: LoadError: cannot load such file -- net/smtp

在Gemfile里加入如下3行即可: gem 'net-smtp' gem 'net-imap' gem 'net-pop' 别忘了重新运行:bundle install
2022-04-29 00:14 | 阅读 581 次 | 评论 0 条

githubu[gitlab]的简易安装记录

export GITLAB_HOME=/var/www/www.githubu.com sudo docker run --detach \ --hostname www.githubu.com \ --publish 443:443 --publish 80:80 --publish 23:23 \ --name gitlab \ --restart always \ --volume $GITLAB_HOME/config:/etc/gitlab \ --volume $GITLAB_HOME/logs:/var/log/gitl...
2021-11-16 20:20 | 阅读 258 次 | 评论 0 条

gem install和bundle install使用代理proxy

假设本地代理服务器的端口是1080 下面是gem的: gem install nokogiri -v '1.10.9' -p http://127.0.0.1:1080 下面是bundle的: export HTTP_PROXY=http://127.0.0.1:1080 bundle install
2020-06-27 06:41 | 阅读 4304 次 | 评论 0 条

语言是思维的记号

选一个好看点的记号,便于梳理思维,有利于身心健康。 乱糟糟的记号把人的思维都带乱了,以其昏昏使人昭昭,@php
2020-05-20 20:05 | 阅读 512 次 | 评论 0 条

基于纯 ruby 的 XML/HTML 解析器,可替代 nokogiri

https://gitlab.com/yorickpeterse/oga
2020-01-08 11:34 | 阅读 1363 次 | 评论 0 条

passenger设置起始进程数

http { passenger_root /usr/local/lib/ruby/gems/2.3.0/gems/passenger-5.0.30; passenger_ruby /usr/local/bin/ruby; passenger_min_instances 3; //此处设置起始进程数为3 passenger_pre_start http://blog.bccn.net/sessions/seccode_check; passenger_max_pool_size 6;...
2019-09-16 00:13 | 阅读 1881 次 | 评论 0 条

textmate2保存文件时自动删除行尾空格

默认textmate2是没有这个功能的,不过可以通过Bundles开启,开启方法: 1、点击菜单中的Bundles,然后 Edit Bundles 2、在弹出窗口中,依次点击 Text -> Menu Actions -> Converting / Stripping -> Remove Trailing Spaces in Document / Selection , 在最右侧的 Semantic Class 一栏填入:callback.document.will-save ,如图所示: ...
2019-07-27 03:00 | 阅读 1606 次 | 评论 0 条

用docker部署rails应用以后要把log和tmp的权限设置为777

cd到rails的目录,执行 chmod 777 -R log chmod 777 -R tmp
2019-05-28 15:03 | 阅读 1110 次 | 评论 0 条

Rails Model中的enum(枚举)二三事

假设这么一个Model class Order < ApplicationRecord enum type: {'支付宝充值': 1, '微信充值': 2, '后台手工加值':3, '后台手工减值':4, '提现':5, '发布问题减值':6, '答案被选中加值':7} end 那么可以进行下面这些枚举操作,首先是对象的操作 [1] pry(main)> order = Order.first => [2] pry(main)> order.type => "后台手工减值" [3] pry(main)> order.read_attribute_before...
2019-05-24 00:36 | 阅读 1185 次 | 评论 0 条

解决ruby内核参考离线版js文件的引用问题

官方的离线版默认是放在根目录的,所以js引用都是这种形式: <script src="/js/extra.js"></script> 注意红色的反斜线,这样如果不是放在根目录,那么就会出现js引用的错误。 废话少说,用一段ruby代码即可解决: files = `ag 'src="/js' -l`.split("\n") files.each do |f| s = open(f).read if f.scan(/\//).count == 0 s = s.gsub(/\<script\s+src=\"...
2019-05-05 18:55 | 阅读 1514 次 | 评论 0 条

rails命令出现大量 warning: constant ::Fixnum is deprecated 的解决办法

这些警告虽然不影响使用,但看着烦人。按以下方法可解决: which rails 找到rails的路径(我的是 /Users/gs/.rvm/gems/ruby-2.5.3/bin/rails,当然你的和我的可能会不一样),然后: vim /Users/gs/.rvm/gems/ruby-2.5.3/bin/rails 在第一行和注释下面加上: ENV['RUBYOPT'] = '-W0' 这样就可以屏蔽warning了
2019-01-21 00:21 | 阅读 7371 次 | 评论 0 条
浏览2617239次
文章归档
最新评论