问题描述:

I have the following which worked in Rails 2.3.8:

/lib/trimmer.rb

I use it in my Contact model as follows:

 include Trimmer #lib module to trim blank spaces in input

But I get this error:

uninitialized constant Contact::Trimmer

I'm guessing lib files are handled differently, but not sure what to change?

 1 module Trimmer

2 # Make a class method available to define space-trimming behavior.

3 def self.included base

4 base.extend(ClassMethods)

5 end

6

7 module ClassMethods

8 # Register a before-validation handler for the given fields to

9 # trim leading and trailing spaces.

10 def trimmed_fields *field_list

11 before_validation do |model|

12 field_list.each do |n|

13 model[n] = model[n].strip if model[n].respond_to?('strip')

14 end

15 end

16 end

17 end

18 end

I think I figured it out...validate?

I have to use require 'trimmer.rb' because in Rails 3, the lib files have to be explicitly required.....

网友答案:

The lib directory isn't loaded by default in rails 3.

You need to add this:

config.autoload_paths += Dir["#{config.root}/lib/**/"]

To your application.rb file.

Then it should work.

相关阅读:
Top