Tag: 初始化程序

Rails 3.1:如何仅为Web应用程序运行初始化程序(rails server / unicorn / etc)

我的webapp需要加密其会话数据。 我设置的是: config/initializers/encryptor.rb: require ‘openssl’ require ‘myapp/encryptor’ MyApp::Encryptor.config[ :random_key ] = OpenSSL::Random.random_bytes( 128 ) Session.delete_all app/models/session.rb: require ‘attr_encrypted’ class Session proc { MyApp::Encryptor.config[ :random_key ] }, :marshal => true # Rest of model stuff end 一切都很好,并保证会话数据安全。 这是问题所在:当我运行自定义rake任务时,它会加载初始化程序并清除所有会话。 不好! 我可以在初始化程序中放置什么来确保它仅用于webapp初始化? 或者,我可以在初始化程序中添加什么来使其不用于rake任务? 更新:好的,我现在所做的是添加MYAPP_IN_RAKE = true unless defined? MYAPP_IN_RAKE MYAPP_IN_RAKE = true unless defined? MYAPP_IN_RAKE到我的.rake文件。 然后在我的初始化程序中我做: unless defined?( […]

如何在Ruby中生成初始化程序?

现在是时候缩短它了: class Foo attr_accessor :a, :b, :c, :d, :e def initialize(a, b, c, d, e) @a = a @b = b @c = c @d = d @e = e end end 我们有’attr_accessor’来生成getter和setter。 我们有什么要按属性生成初始值设定项吗?