gpt4 book ai didi

ruby-on-rails-3 - 调用 expire_fragment 引发 "to use #url_for, you must include routing helpers explicitly"错误

转载 作者:行者123 更新时间:2023-12-03 23:36:45 29 4
gpt4 key购买 nike

我一直在将 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_helpers,而它应该能够很好地使用 url_for?

最佳答案

我也遇到了这个错误,但是在我的集成和功能测试中,甚至在使用简单的 link_to 助手的 View 中。这篇文章让我走上了检查我的 gem 依赖项的道路:Routes stopped working when upgrading Rails 3.0 to 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

关于ruby-on-rails-3 - 调用 expire_fragment 引发 "to use #url_for, you must include routing helpers explicitly"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17553326/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com