问题
I'm trying to get the to_prepare event to work on a new Rails 3.2.1 project. I've placed the following:
Rails.application.config.to_prepare do
puts 'here i am before a request'
end
into an initializer under config/initializers. According to the documentation here, this block should run on every request to the app when running in development mode, and only once in production. I'm working in development mode, and this block does not run on every request, instead it runs only when I boot up the application, and never again.
The following is a sample of output from when I load the app.
rails s
=> Booting WEBrick
=> Rails 3.2.1 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
here i am before a request
[2012-03-02 20:29:46] INFO WEBrick 1.3.1
[2012-03-02 20:29:46] INFO ruby 1.9.2 (2011-07-09) [x86_64-darwin11.2.0]
[2012-03-02 20:29:46] INFO WEBrick::HTTPServer#start: pid=37897 port=3000
When I make subsequent requests, the string 'here i am...' is not displayed, only the regular output from the Rails log. All my searching has only mentioned documentation that seems to tell me to do things this way. Is there anything I might be missing?
回答1:
to_prepare is called every time your project is reloaded, which since Rails 3.2 means every time you change a file.
refs: https://github.com/rails/rails/issues/7152#issuecomment-8397470
来源:https://stackoverflow.com/questions/9542600/using-rails-to-prepare-event