2014-12-07 52 views
0

当我运行从默认模块的代码,我希望它通过mattr_accessor设置变量:设置变量并不如预期

Jsonforem.user_class = "User" 

然后运行foreman start,我得到这个错误我外壳:

22:49:40 web.1 | [44763] ! Unable to load application: NoMethodError: undefined method `user_class=' for Jsonforem:Module 

下面是我的模块代码:

require "jsonforem/engine" 

module Jsonforem 

mattr_accessor :user_class, :user_profile_links, :email_from_address, 
      :per_page, :sign_in_path, :moderate_first_post 

def user_class 
    if @@user_class.is_a?(String) 
    begin 
     Object.const_get(@@user_class) 
    rescue NameError 
     @@user_class.constantize 
    end 
    end 
end 

end 

所以发R,我能得到它的工作的唯一办法是这样的:

module Jsonforem 
    user_class = "User" 
end 

为什么上面的代码块的工作,但我无法从模块外部设置的变量?如果你需要情况下,我在做什么在这里建议您的红宝石宝石镶嵌配置变量:http://guides.rubyonrails.org/engines.html

干杯

回答

0

哪里是你的模块中的lib目录?
您的模块应该在之前加载。 把它放在config/initializers中,然后尝试。

+0

该模块包含的宝石内。 – Ryan 2014-12-07 23:38:58

0

需要解决我的问题:

require "jsonforem" 
Jsonforem.user_class = "User"