2012-08-05 33 views
0

嘗試在rails中創建觀察器並獲取錯誤{/active_support/dependencies.rb:503:in'load_missing_constant':預期定義(LoadError)}ActiveSupport依賴關係(load_missing_constant)錯誤 - 期望...定義(加載錯誤)

使用Rails 3.2.6和Ruby 1.9.3-p194(Lion上的rvm)。

在評論application.rb中這條線可以防止錯誤(也是預期的行爲) config.active_record.observers =:page_observer

'軌道服務器' 命令的完整輸出:

=> Booting WEBrick 
=> Rails 3.2.6 application starting in development on http://0.0.0.0:3000 
=> Call with -d to detach 
=> Ctrl-C to shutdown server 
Exiting 
/Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:503:in 'load_missing_constant': Expected /Users/d/Dropbox/Projects/Postsocial/postsocial/app/models/page.rb to define Page (LoadError) 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:192:in 'block in const_missing' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:190:in 'each' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:190:in 'const_missing' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:514:in 'load_missing_constant' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:192:in 'block in const_missing' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:190:in 'each' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:190:in 'const_missing' 
from /Users/d/Dropbox/Projects/Postsocial/postsocial/app/models/page_observer.rb:2:in '<class:PageObserver>' 
from /Users/d/Dropbox/Projects/Postsocial/postsocial/app/models/page_observer.rb:1:in '<top (required)>' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:469:in 'load' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:469:in 'block in load_file' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:639:in 'new_constants_in' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:468:in 'load_file' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:353:in 'require_or_load' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:502:in 'load_missing_constant' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:192:in 'block in const_missing' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:190:in 'each' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:190:in 'const_missing' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/inflector/methods.rb:229:in 'block in constantize' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/inflector/methods.rb:228:in 'each' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/inflector/methods.rb:228:in 'constantize' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/core_ext/string/inflections.rb:54:in 'constantize' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.6/lib/active_model/observing.rb:86:in 'instantiate_observer' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.6/lib/active_model/observing.rb:59:in 'block in instantiate_observers' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.6/lib/active_model/observing.rb:59:in 'each' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.6/lib/active_model/observing.rb:59:in 'instantiate_observers' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/railtie.rb:117:in 'block (2 levels) in <class:Railtie>' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/lazy_load_hooks.rb:36:in 'instance_eval' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/lazy_load_hooks.rb:36:in 'execute_hook' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/lazy_load_hooks.rb:43:in 'block in run_load_hooks' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/lazy_load_hooks.rb:42:in 'each' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/lazy_load_hooks.rb:42:in 'run_load_hooks' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/base.rb:721:in '<top (required)>' 
from /Users/d/Dropbox/Projects/Postsocial/postsocial/app/models/page.rb:1:in '<top (required)>' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:469:in 'load' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:469:in 'block in load_file' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:639:in 'new_constants_in' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:468:in 'load_file' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:353:in 'require_or_load' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:502:in 'load_missing_constant' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:192:in 'block in const_missing' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:190:in 'each' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:190:in 'const_missing' 
from /Users/d/Dropbox/Projects/Postsocial/postsocial/config/routes.rb:60:in 'block in <top (required)>' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/routing/route_set.rb:282:in 'instance_exec' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/routing/route_set.rb:282:in 'eval_block' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.6/lib/action_dispatch/routing/route_set.rb:260:in 'draw' 
from /Users/d/Dropbox/Projects/Postsocial/postsocial/config/routes.rb:1:in '<top (required)>' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:245:in 'load' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:245:in 'block in load' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:236:in 'load_dependency' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:245:in 'load' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/application/routes_reloader.rb:40:in 'block in load_paths' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/application/routes_reloader.rb:40:in 'each' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/application/routes_reloader.rb:40:in 'load_paths' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/application/routes_reloader.rb:16:in 'reload!' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/application/routes_reloader.rb:26:in 'block in updater' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/file_update_checker.rb:78:in 'call' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/file_update_checker.rb:78:in 'execute' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/application/routes_reloader.rb:27:in 'updater' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/application/routes_reloader.rb:7:in 'execute_if_updated' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/application/finisher.rb:66:in 'block in <module:Finisher>' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/initializable.rb:30:in 'instance_exec' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/initializable.rb:30:in 'run' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/initializable.rb:55:in 'block in run_initializers' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/initializable.rb:54:in 'each' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/initializable.rb:54:in 'run_initializers' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/application.rb:136:in 'initialize!' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/railtie/configurable.rb:30:in 'method_missing' 
from /Users/d/Dropbox/Projects/Postsocial/postsocial/config/environment.rb:5:in '<top (required)>' 
from /Users/d/Dropbox/Projects/Postsocial/postsocial/config.ru:4:in 'require' 
from /Users/d/Dropbox/Projects/Postsocial/postsocial/config.ru:4:in 'block in <main>' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/builder.rb:51:in 'instance_eval' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/builder.rb:51:in 'initialize' 
from /Users/d/Dropbox/Projects/Postsocial/postsocial/config.ru:1:in 'new' 
from /Users/d/Dropbox/Projects/Postsocial/postsocial/config.ru:1:in '<main>' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/builder.rb:40:in 'eval' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/builder.rb:40:in 'parse_file' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/server.rb:200:in 'app' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/commands/server.rb:46:in 'app' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/server.rb:301:in 'wrapped_app' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.1/lib/rack/server.rb:252:in 'start' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/commands/server.rb:70:in 'start' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/commands.rb:55:in 'block in <top (required)>' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/commands.rb:50:in 'tap' 
from /Users/d/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.6/lib/rails/commands.rb:50:in '<top (required)>' 
from script/rails:6:in 'require' 
from script/rails:6:in '<main>' 

page_observer.rb的內容

class PageObserver < ActiveRecord::Observer 
end 

page.rb

內容
class Page < ActiveRecord::Base 
    attr_accessible :content, :page_style, :slug, :title 
end 

任何想法,將不勝感激。

回答

0

application.rb中 config.active_record.observers =:page_observer

page_observer.rb

observe :page 

嘗試初始化您的觀察員application.rb中你還應該指定你的方法之前需要觀察。

你缺少什麼行爲?

+0

我在application.rb中初始化了它。這就是拋出錯誤的原因。 – lofdev 2012-08-05 01:44:06

+0

上述問題已解決,請訪問http://railsforum.com/viewtopic.php?id=16725 – 2012-08-05 02:06:01