[英]Call to expire_fragment raises “ to use #url_for, you must include routing helpers explicitly” error
我一直在将Rails 2应用程序升级到Rails 3.2.13,并且在尝试启用缓存时出现问题。 缓存在Rails 2中工作,我使用的是相同版本的Ruby - 1.8.7。 我不确定它是否相关,但我正在开发OSX。
调用expire_fragment时,将从ActionController :: Caching :: Fragments类抛出错误。 expire_fragment调用fragment_cache_key,其中包含:
ActiveSupport::Cache.expand_cache_key(key.is_a?(Hash) ? url_for(key).split("://").last : key, :views)
对url_for的调用引发了此错误
2013-07-10T14:40:50.430137+01:00 FATAL
RuntimeError (In order to use #url_for, you must include routing helpers explicitly. For instance, `include Rails.application.routes.url_helpers):
lib/bio_catalogue/cache_helper.rb:175:in `expire_fragment'
lib/bio_catalogue/cache_helper.rb:181:in `expire_service_index_tag_cloud'
app/observers/annotation_observer.rb:33:in `expire_caches'
app/observers/annotation_observer.rb:12:in `after_create'
app/controllers/annotations_controller.rb:140:in `create_inline'
这是进一步延伸的回溯:
freya.2235 actionpack (3.2.13) lib/abstract_controller/url_for.rb:14:in `_routes'
freya.2235 actionpack (3.2.13) lib/action_dispatch/routing/url_for.rb:148:in `url_=
freya.2235 for'
freya.2235 actionpack (3.2.13) lib/action_controller/caching/fragments.rb:53:in `f=
freya.2235 ragment_cache_key'
freya.2235 actionpack (3.2.13) lib/action_controller/caching/fragments.rb:112:in `=
freya.2235 expire_fragment'
freya.2235 lib/bio_catalogue/cache_helper.rb:175:in `expire_fragment'
freya.2235 lib/bio_catalogue/cache_helper.rb:181:in `expire_service_index_tag_clou=
freya.2235 d'
freya.2235 app/observers/annotation_observer.rb:33:in `expire_caches'
freya.2235 app/observers/annotation_observer.rb:12:in `after_create'
freya.2235 activemodel (3.2.13) lib/active_model/observing.rb:231:in `send'
freya.2235 activemodel (3.2.13) lib/active_model/observing.rb:231:in `update'
freya.2235 activerecord (3.2.13) lib/active_record/observer.rb:114:in `_notify_ann=
freya.2235 otation_observer_for_after_create'
当然,我试图在调用expire_fragment的类中包含Rails.application.routes.url_helpers,但无效,因为错误来自ActionController模块。 ActionController :: Base已经包含了UrlFor模块,所以我的问题是:为什么包含UrlFor的模块类会引发错误,要求在应该能够使用url_for时包含url_helpers?
我也遇到了这个错误,但是在我的集成和功能测试中,甚至在使用简单的link_to帮助器的视图中。 这篇文章让我顺着检查我的gem依赖关系的路径: 在将Rails 3.0升级到3.1时,路由停止工作
我能够通过更改以下内容解决问题:FROM:
group :development, :test do
gem 'mocha', '0.10.0', :require => false
gem 'vcr', '1.11.3'
gem 'fakeweb', '1.3.0'
gem 'capistrano', '2.13.5'
gem 'ruby-debug-ide', '0.4.7'
gem 'letter_opener', '1.1.0'
end
至:
group :development, :test do
gem 'mocha', '~> 0.14', :require => false
gem 'vcr', '~> 2.5'
gem 'fakeweb', '1.3.0'
gem 'capistrano', '~> 2.15'
gem 'ruby-debug-ide', '0.4.17', :require => false
gem 'letter_opener', '~> 1.1'
end
我也遇到了同样的问题,如错误提到包含以下url_helper模块对我不起作用。
include Rails.application.routes.url_helpers
所以我使用自定义网址而不是路由帮助方法
messages_path(@message)
更改为"/messages/@message.id"
这适合我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.