2010-08-23 21 views
0

我不知道我在做什么错在这里,我有一个文件在lib/acts_as_votable.rb,它只是一个投票系统的应用程序。正确加载Rails的红宝石模块

module ActsAsVotable 

end 

module ActiveRecord 
    class Base 
    class << self 
     cattr_accessor :votable 

     def acts_as_votable 
     has_many :votes, :as => :voteable 
     end 

     def votable? 
     method_defined? :votes 
     end 
    end 

    def votable? 
     self.class.send(:method_defined?, :votes) 
    end 
    end 
end 

但似乎从来没有模块加载:

undefined local variable or method `acts_as_votable' for #<Class:0x00000101796d80> 

会是怎样加载模块的正确方法?

+0

您正在使用哪种rails版本? – 2010-08-23 08:54:11

+0

我正在运行1.9.2-head – 2010-08-23 15:35:32

+0

据我所知,Rails 3不会从'lib'预装文件。 – 2010-08-23 16:01:52

回答

2

您可以将您的扩展名放在config/initializers目录中,这样它们将被Rails自动预加载。

+0

嗯......这确实有效,但这是最佳做法吗? – 2010-08-27 17:45:30

+0

我认为这是。 Rails转向加载初始化器。初始化程序是存储在应用程序中/ config/initializers下的任何ruby代码文件。您可以使用初始化程序来保存所有框架和插件加载后应该进行的配置设置。“http://guides.rubyonrails.org/configuring.html。 – 2010-08-27 17:54:14

0

不确定OP的所需功能是否与配置有关。要在/ lib中加载文件,您可以将以下内容添加到/config/application.rb:

config.autoload_paths += %W(#{config.root}/lib/)