用例见注释:
#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[...
比如User模型,则用:
User.validators
在rails开发中,有时候修改了assets里的静态文件名不生效,可能是Sprockets缓存的问题,可以用下面的命令清空:
rake tmp:cache:clear
然后再运行 rails s 即可,初次运行后刷新页面需要很长时间,因为缓存被清空了第一次要建立缓存,后面再刷新就很快了
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...
生成模型
rails g model User username:string email:string password_digest:string
在user模型加入has_secure_password
class User < ApplicationRecord
has_secure_password
end
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())
})
The ActiveRecord data types available in Rails 5.
:primary_key
:string
:text
:integer
:bigint
:float
:decimal
:numeric
:datetime
:time
:date
:binary
:boolean
...
触发Validation
触发Callbacks
改动updated_at
update
✅
✅
✅
update_attribute
❌
✅
✅
...
执行完
rake db:migrate
以后再执行
rake db:schema:dump
在Gemfile里加入如下3行即可:
gem 'net-smtp'
gem 'net-imap'
gem 'net-pop'
别忘了重新运行:bundle install
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...
假设本地代理服务器的端口是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
选一个好看点的记号,便于梳理思维,有利于身心健康。
乱糟糟的记号把人的思维都带乱了,以其昏昏使人昭昭,@php
https://gitlab.com/yorickpeterse/oga
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;...
默认textmate2是没有这个功能的,不过可以通过Bundles开启,开启方法:
1、点击菜单中的Bundles,然后 Edit Bundles
2、在弹出窗口中,依次点击 Text -> Menu Actions -> Converting / Stripping -> Remove Trailing Spaces in Document / Selection , 在最右侧的 Semantic Class 一栏填入:callback.document.will-save ,如图所示:
...
cd到rails的目录,执行
chmod 777 -R log
chmod 777 -R tmp
假设这么一个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...
官方的离线版默认是放在根目录的,所以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=\"...
这些警告虽然不影响使用,但看着烦人。按以下方法可解决:
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了