config.i18n.fallbacks = true 。production 下的語系順序才會乖乖實現。
config.i18n.fallbacks = true 。![]() |
| (轉自: ROR實戰聖經) |
user deployer; # 定義操作 nginx 的使用者
worker_processes 1; # 定義 worker 的數量
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_proxied any;
gzip_vary off;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/rss+xml application/atom+xml text/javascript application/javascript application/json text/mathml;
gzip_min_length 1000;
gzip_disable "MSIE [1-6]\.";
server_names_hash_bucket_size 64;
types_hash_max_size 2048;
types_hash_bucket_size 64;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
upstream unicorn {
server 127.0.0.1:8080 fail_timeout=0;
}
server {
listen 80 default deferred;
server_name flytutor.com;
root /var/www/flytutor/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://unicorn;
}
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
upstream flytutor.com {
server 192.168.0.111:8080 weight=3;
server 192.168.0.222:8080 weight=2;
server 192.168.0.333:8080 weight=3;
}
其中 weight 代表的是「被配發 request 的權重」,當 weight 越高的時候被分配到的機率越大,可以做 loading balance。 upstream unicorn {
server unix:/tmp/unicorn.sock fail_timeout=0;
}
app_root = "/var/www" app_name = "flytutor" listen "127.0.0.1:10101" #, :backlog => 2048 #這邊要跟nginx虛擬主機檔中upstream內定義的務必一樣 worker_processes 2 #看情況開 preload_app false timeout 30 module Rails class <<self def root File.expand_path(__FILE__).split('/')[0..-3].join('/') end end end _working_directory = File.join(app_root, app_name) working_directory _working_directory logs_path = "#{_working_directory}/log" pid "#{_working_directory}/tmp/pids/unicorn.pid" stderr_path "#{logs_path}/unicorn.stderr.log" stdout_path "#{logs_path}/unicorn.stdout.log" GC.respond_to?(:copy_on_write_friendly=) and GC.copy_on_write_friendly = true before_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! old_pid = "#{Rails.root}/tmp/pids/unicorn.pid.oldbin" if File.exists?(old_pid) && server.pid != old_pid begin Process.kill("QUIT", File.read(old_pid).to_i) rescue Errno::ENOENT, Errno::ESRCH puts "Send 'QUIT' signal to unicorn error!" end end end after_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection end
/usr/bin/unicorn_rails -c config/unicorn.rb -E $RAILS_ENV -D。但現在 unicorn 官方已經不建議使用這種作法而是直接改用 unicorn 來啟動/重啟 unicorn。$ gem install vagrant #安裝 vagrant $ vagrant box add ubuntu http://cloud-images.ubuntu.com/vagrant/quantal/current/quantal-server-cloudimg-i386-vagrant-disk1.box #安裝新的 Vagrant Package。這裡的 ubuntu 是一個預先做好的空的 ubuntu 12.10 (intel-based) $ vagrant init ubuntu $ vagrant upbox 檔可以在 http://www.vagrantbox.es/ 下載
config.vm.network :hostonly, "33.33.33.33" 將ip改成自己想要的ip,這裡以 "33.33.33.33" 為例。修改完後要執行 vangrant reload [default] Failed to connect to VM!當出現上面的錯誤訊息時,可以照 http://vagrant.wikia.com/wiki/Usage 上的步驟來排除,基本上就是將 Vagrantfile 中的
Failed to connect to VM via SSH. Please verify the VM successfully booted
by looking at the VirtualBox GUI.
config.vm.boot_mode = :gui 設定打開。再重新 vagrant up,在產生的 GUI 中輸入 sudo dhclient eth0。[default] The guest additions on this VM do not match the install version of
VirtualBox! This may cause things such as forwarded ports, shared
folders, and more to not work properly. If any of those things fail on
this machine, please update the guest additions and repackage the
box.
User.all.pluck :name # => ["Kevin", "Laura", "Yiya", "Diya"]Order.where( :product_id => Product.where("price<1000") )Model.where("state == 'decline'").update_all(:state => 'deny')has_many :cancled_orders, :class_name=>"Order", :conditions=>proc{ "status = 'Cancled'" }
has_many :handling_orders, :class_name=>"Order", :conditions=>proc{ "status = 'Handling'" }
has_many :paid_orders, :class_name=>"Order", :conditions=>proc{ "status = 'Paid'" }
current_user.cancled_orders 的方式取到「取消的訂單」了,相當直觀。t = Post.arel_table
results = Post.where(
t[:author].eq("Someone").
or(t[:title].matches("%something%"))
)
rails plugin new <plugin_name>,會產生 plugin 資料夾,結構為:my_plugin
├── Gemfile
├── Gemfile.lock
├── MIT-LICENSE
├── README.rdoc
├── Rakefile
├── lib
│ ├── my_plugin
│ │ └── version.rb
│ ├── my_plugin.rb
│ └── tasks
│ └── my_plugin_tasks.rake
├── my_plugin.gemspec
└── test
│
...(本篇不提到,忽略)
module ZurbFoundation class Engine < Rails::Engine end end你可以將他獨立成一個檔案由
my_plugin.rb 載入,或是直接寫到 my_plugin.rb 裡。有了這段程式碼,Rails就會自動將 app 及 config 兩個資料夾下的所有檔案載入。請注意,載入 engine 的 code 必須放在 my_plugin.rb 的最後,才能正常運作
# CURRENT FILE :: app/controllers/my_plugin/my_controller.rb module MyPlugin class MyController < ::ApplicationController def index ... end end end
# CURRENT FILE :: config/routes.rb Rails.application.routes.draw do get "team" => "team_page/team#index" , :as => :team_page end
gem build /project_path/user_switch.gemspec打包後的 gem 可以 push 到 rubygems.org:
gem push project_name-0.0.1.gem
gem "my_plugin", :path=>"/path/to/your/plugin"
gem "my_plugin", :git=>"https://path/to/your/repo"
gem "my_plugin"
YourUser::Fileclass is not loaded. You have to require it (e.g. inuser.rb).The following happens when ruby/rails seesUser::Infoand evaluates it (simplified; onlyUseris defined yet).
- check if
User::Infois defined - it is not (yet)- check if
Infois defined - it is not (yet)uninitialized constant-> do rails magic to find theuser/info.rbfile and require it- return
User::InfoNow lets do it again forUser::File
- check if
User::Fileis defined - it is not (yet)- check if
Fileis defined - it is (because ruby has a built inFileclass)!- produce a warning, because we've been asked for
User::Filebut got::File- return
::File