Ruby on Rails - 模块配置实例变量意外地设置为nil

时间:2018-05-25 00:47:12

标签: ruby-on-rails ruby

我在Ruby on Rails中处理奇怪的问题。
app / lib / cml.rb 中我有:

module CML
  class << self
    attr_accessor :scheduler
    def configure
      yield self
      true
    end
  end
end

然后我在 config / initializers / cml.rb

CML.configure do |config|
  config.scheduler = "hey"
end

应用程序/控制器/ example_controller.rb

class ExampleController < ApplicationController
    def action1
        logger.debug "Scheduler: #{CML.scheduler}"
        puts ""
    end
end

应用程序/配置/ routes.rb中

Rails.application.routes.draw do
  get '/action1', to: 'example#action1'
end

问题:

  1. 启动服务器
  2. 从example_controller.rb#action1
  3. 中删除puts行
  4. request / action1
  5. 预期行为:在日志中我看到&#34;调度程序:嘿&#34;。
    现实:在日志中,我看到&#34;调度程序:&#34; (在byebug中我看到它没有了)

    使用示例应用的Github回购:https://github.com/luki215/rails-module-error

    我做错了什么?我看到这种配置常用于几个宝石,这些宝石工作得很好。谢谢!

0 个答案:

没有答案