Author Archives: vvpick

捕获 ActiveRecord::RecordNotFound 输出 404 页面

class ApplicationController < ActionController::Base protect_from_forgery with: :exception rescue_from ActiveRecord::RecordNotFound, with: :render_404 protected def render_404 render(file: “#{Rails.root}/public/404.html”, :layout => false, :status => 404) end end

Posted in ruby on rails | Tagged , , | Leave a comment

Rails 4 Tip

RAILS_ROOT 被 Rails.root 替换 ActiveRecord 的 White list attributes 被 Strong Parameters Gem 替换 如果使用了 accepts_nested_attributes_for 应当如下使用 `Strong Parameters` Class User accepts_nested_attributes_for :profile end # strong parameters params.require(:user).permit(:name, profile_attributes: [:xxx, xx])

Posted in ruby on rails | Leave a comment

CarrierWave README

安装 安装最新稳定版 [sudo] gem install carrierwave 下面代码加入 Gemfile gem ‘carrierwave’ 重启 server CarrierWave 0.5 不兼容 Rails 2,如果你使用的是 Rails 2,请使用 github 仓库中的 CarrierWave 0.4-stable 分支 开始使用 生成一个 uploader rails generate uploader Avatar 将会生成下面文件 app/uploaders/avatar_uploader.rb 使用 uploader 类接收和保存文件 uploader = AvatarUploader.new uploader.store!(my_file) … Continue reading

Posted in ruby on rails | Tagged | Leave a comment

Ruby 生成随机唯一字符串

Time.now.utc.to_i.to_s + ‘-’ + Process.pid.to_s + ‘-’ + (“%04d” % rand(9999)) 随机性可能不太好,唯一没问题

Posted in ruby on rails | Tagged , | Leave a comment

Linux 下面 cp/mv 为什么没有进度显示

为什么要避免在 cp 时显示进度条这种设计? 进度条显示依赖于两个关键,一是总工作量,一是当前进度量。 对于 wget 来说, 如果要获取的资源在头部有 Content-Length, 那么它可以认为获得了“总工作量”, 而具体下载过程中下载了多个字节它是知道的,这种情况下得到进度百分比是轻负担的,可承受的。 对于wget获取一个使用 http 协议的 URL 来说, 如果 Content-Length 没有指定, 则wget 也无法获取总工作量, 它所能做的只是下载一点是一点,直到收到一个结束标志。而这种情况下即使wget也无法正确显示进度条。 注: Content-Length 头在 Http 协议中是可选的. 对 于cp -a 这样的任务来说, 可能很多用把它用作备份的手段, 执行这个命令一般都是大宗买卖, 源往往是一个目录。 而获取这样一个目录的总大小是可能但不可负担的,你可以 du -csh /usr/src/linux 试试看。 在这段时间内你差不多可以复制完成了。 … Continue reading

Posted in Linux | Tagged , , | Leave a comment

namesilo 的域名解析 ttl 默认时间48小时

48小时。。。。。 好吧,我忍,最小值3600(1个小时)

Posted in Internet | Tagged , | Leave a comment

ubuntu 无法使用php gettext的问题

今天把一台 php server 迁移服务器,新服务器使用 ubuntu,老的是 centos,在 centos下无问题, 迁移到 ubuntu 之后 php gettext 就是无法使用,表现为无法翻译内容,显示的内容还是老的。 经过检查 php-gettext gettext 安装都正常 排查之后发现是 ubuntu 默认不支持 zh_CN 语言字符集 这里要说明一下,使用 gettext 是需要依赖系统支持的语言集,如果系统不支持 zh_CN,翻译成这种 语言根本也就无法使用了 给 ubuntu 添加 zh_CN 语言支持 在 /var/lib/locales/supported.d/local 文件中添加新行,内容如下 zh_CN UTF-8 然后运行 dpkg-reconfigure locales … Continue reading

Posted in Linux, PHP | Tagged , , | Leave a comment

sudo cd 为什么不能工作

sudo cd 为什么不能工作 因为 cd 是 shell 内置的, 而 sudo 只能对可执行的文件起作用,也就是说在硬盘中根本没有 cd 这个文件(命令) 怎么解决呢,可以通过使用 sudo sh -c ‘cd dirname’ 或者 sudo -s 之后进入 root shell 之后在执行 cd dirname

Posted in Linux | Tagged , | Leave a comment

nginx client_max_body_size 的问题

遇到上传文件问题 “413 Request Entity Too Large” 查到了 nginx client_max_body_size ,原来默认值只有 1m,太小了 增大他,在 location 中添加下面设置 client_max_body_size 6m; nginx reload, 不生效, 再试,把上面设置放 server 中,还不生效,继续放 http 中,这回生效了 这是什么问题,nginx 的文档上面明明写着 他可以放在 http server location 里面的 这是 nginx 的 bug,还是 nginx 的特性 如果是特性的话我想 http server … Continue reading

Posted in Linux | Tagged , | Leave a comment

ActionDispatch::RemoteIp::IpSpoofAttackError

使用 unicorn + nginx 运行 rails 有一段时间了,之前 nginx 这样配置 ip 地址 proxy_set_header X-Real-IP $remote_addr; 发现 rails 中获取到的 ip 是 127.0.0.1 , 然后又配置 CLIENT_IP proxy_set_header CLIENT_IP $remote_addr; 这样配置之后就发现了 rails 的错误 ActionDispatch::RemoteIp::IpSpoofAttackError 原来 rails 有 ip 防欺骗,再改 ningx 配置,加入 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; … Continue reading

Posted in ruby on rails | Tagged , , , | Leave a comment