Category Archives: ruby on rails

Ruby on Rails

Ruby 百分号创建数组小写 w 和大写的区别

%W 使用双引号处理内容 %w 使用单引号处理内容 看例子: myvar = ‘one’ p %w{#{myvar} two three 1 2 3} # => ["\#{myvar}", "two", "three", "1", "2", "3"] p %W{#{myvar} two three 1 2 3} # => ["one", "two", "three", "1", "2", "3"]

Posted in ruby on rails | Tagged | Leave a comment

Mac OS X 安装 mysql2

近日将 Mac OS 10.7 升级到 OS X 10.8,安装 ruby mysql2 gem 出现下面问题: Building native extensions. This could take a while… ERROR: Error installing mysql2: ERROR: Failed to build gem native extension. checking for rb_thread_blocking_region()… *** extconf.rb failed *** Could not … Continue reading

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

Capistrano restart Passenger Server

desc “Restart Application” task :restart do run “touch #{current_release}/tmp/restart.txt” end

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

config.threadsafe! Rails 中的多进程和多线程(翻译)

原文写在 Rails 3时代,目前 Rails 4已经去除 config.threadsafe!,默认已经是线程安全的。 CONFIG.THREADSAFE!: WHAT DOES IT DO? def threadsafe! @preload_frameworks = true @cache_classes = true @dependency_loading = false @allow_concurrency = true self end Preloading Frameworks The first option @preload_frameworks does pretty much what it says, it … Continue reading

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

捕获 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

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

Rails gsub 的坑 (SafeBuffer 相关)

代码一 s = “[a1111][b22222][v4321342]” s.gsub(/\[\w(\d+)\]/) do $1 end 结果 => “1111222224321342″ 代码二 s = ERB::Util.html_escape(“[a1111][b22222][v4321342]“) s.gsub(/\[\w(\d+)\]/) do $1 end 结果 => “432134243213424321342″ 怎么样,很不一样哈 一开始想 html_escape 就是转换几个字符,怎么会出现这样的情况,仔细查看之后 发现 html_escape 返回的是 SafeBuffer 对象,就是这个 SafeBuffer 导致的这个问题, 使用 to_s 还不行,原来 SafeBuffer 重写了 to_s def … Continue reading

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