调用expire_fragment会引发“要使用#url_for,您必须明确地包括路由助手.错误 [英] Call to expire_fragment raises " to use #url_for, you must include routing helpers explicitly" error

查看:154
本文介绍了调用expire_fragment会引发“要使用#url_for,您必须明确地包括路由助手.错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在将Rails 2应用程序升级到Rails 3.2.13,并且在尝试启用缓存时遇到问题.缓存在Rails 2中工作,我使用的是相同版本的Ruby-1.8.7.我不确定是否相关,但是我正在OSX上进行开发.

I've been upgrading a Rails 2 app to Rails 3.2.13 and am having a problems when I try to enable caching. Caching worked in Rails 2 and I'm using the same version of Ruby - 1.8.7. I'm not sure if it is relevant but I'm developing on OSX.

当调用expire_fragment时,从ActionController :: Caching :: Fragments类抛出错误. expire_fragment调用包含以下内容的fragment_cache_key:

The error's being thrown from the ActionController::Caching::Fragments class when expire_fragment is called. expire_fragment makes a call to fragment_cache_key which contains this:

ActiveSupport::Cache.expand_cache_key(key.is_a?(Hash) ? url_for(key).split("://").last : key, :views)

对此url_for的调用引发此错误

This call to url_for is raising this error

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'

这是进一步延伸的回溯:

This is the backtrace that extends a little further:

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?

Naturally I have tried to include Rails.application.routes.url_helpers in the class calling expire_fragment but to no avail as the error is coming from within the ActionController module. ActionController::Base already has the UrlFor module included in it so my question is: Why does a class of a module that includes UrlFor raise an error asking for url_helpers to be included when it should be able to use url_for just fine?

推荐答案

我也遇到了同样的问题,因为错误地提到要包含以下url_helper模块对我不起作用.

I am also faced the same issue, as mentioned in error to include following url_helper module doesn't work for me.

include Rails.application.routes.url_helpers

所以我使用了自定义网址,而不是像这样的路由帮助器方法.

So I have used custom urls instead of route helper methods like

messages_path(@message)更改为"/messages/@message.id"

这对我有用.

这篇关于调用expire_fragment会引发“要使用#url_for,您必须明确地包括路由助手.错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆