如果大家想在 rails 启动的时候做点额外的事,可以参考下面的方法,在合适的文件中加入代码。 在 config/boot.rb 中加入以下代码,基本思想是重写 require 函数
# config/boot.rb
alias :origin_require :require
def require_out(file)
puts file if file.include?("html-demo") #参数为项目的名称
origin_require(file)
end
alias :require :require_out
运行 rails s 输出的结果
/home/lzj/workspace/rails/html-demo/config/application
/home/lzj/workspace/rails/html-demo/config/boot
=> Booting Thin
=> Rails 3.2.11 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
/home/lzj/workspace/rails/html-demo/config/environment
/home/lzj/workspace/rails/html-demo/config/application
/home/lzj/workspace/rails/html-demo/config/environments/development.rb
>> Thin web server (v1.5.0 codename Knife)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop
运行 rails s -e production 输出的结果
/home/lzj/workspace/rails/html-demo/config/application
/home/lzj/workspace/rails/html-demo/config/boot
=> Booting Thin
=> Rails 3.2.11 application starting in production on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
/home/lzj/workspace/rails/html-demo/config/environment
/home/lzj/workspace/rails/html-demo/config/application
/home/lzj/workspace/rails/html-demo/config/environments/production.rb
/home/lzj/workspace/rails/html-demo/app/controllers/application_controller
/home/lzj/workspace/rails/html-demo/app/helpers/application_helper
/home/lzj/workspace/rails/html-demo/app/helpers/demo_helper
/home/lzj/workspace/rails/html-demo/app/controllers/demo_controller
/home/lzj/workspace/rails/html-demo/app/models/user
>> Thin web server (v1.5.0 codename Knife)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop