尝试在rails中创建一个Observer,并在'load_missing_constant'中出现错误{/active_support/dependencies.rb:503:期望定义(LoadError)}
使用Rails 3.2.6,使用Ruby 1.9.3-p194(rvm on Lion)。
在application.rb中注释这一行可以防止错误(还有预期的行为) config.active_record.observers =:page_observer
'rails server'命令的完整输出:
=> 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 :(得分:0)
application.rb中 config.active_record.observers =:page_observer
page_observer.rb
observe :page
尝试在application.rb中初始化您的观察者您还应该在方法之前指定要观察的内容。
你缺少什么行为?