Rails 服务器启动后如何运行我的 ruby​​ 代码? [英] How to run my ruby code after Rails server start?

查看:38
本文介绍了Rails 服务器启动后如何运行我的 ruby​​ 代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过了:

after_initialize do
  #code
end

但是:(文档)

您的应用程序的某些部分,尤其是观察者和路由,不是尚未在调用 after_initialize 块的地方设置.

Some parts of your application, notably observers and routing, are not yet set up at the point where the after_initialize block is called.

我的代码中需要路由和记录器

I need routing and logger in my code

有什么想法吗?

推荐答案

参见 http://的第 3.1 节guides.rubyonrails.org/configuring.html

我相信你会把这段代码放在 config/application.rb 中

I beleive you would put this code in config/application.rb

config.after_initialize do
    # ....
end

# config.after_initialize takes a block which will be run after Rails has finished initializing the application. 
# That includes the initialization of the framework itself

还有 http://guides.rubyonrails.org/initialization.html

这篇关于Rails 服务器启动后如何运行我的 ruby​​ 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆