3

我一直在升級Rails 2應用到Rails 3.2.13,並且在嘗試啓用緩存時遇到問題。緩存在Rails 2中工作,我使用的是相同版本的Ruby - 1.8.7。我不確定它是否相關,但我正在OSX上開發。調用expire_fragment引發「使用#url_for,你必須明確包含路由助手」錯誤

當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的情況下?

+1

我們在將它發送到expire_fragment之前通過查找url_for片段找到了解決方法。例如從 @ controller.expire_fragment(key,options) 到 @ controller.expire_fragment(Rails.application.routes.url_helpers.url_for(key.merge({:only_path => true})),options) – Njall

回答

0

我也遇到了這個錯誤,但在我的集成和功能測試中,甚至在使用簡單link_to助手的視圖中。這篇文章派我來檢查我的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 

TO:

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 
0

我也面臨同樣的問題,正如錯誤中提到的那樣,以下url_helper模塊不適用於我。

include Rails.application.routes.url_helpers 

所以我使用了自定義的網址,而不是路由幫助方法,如

messages_path(@message)改爲"/messages/@message.id"

這對我的作品。

相關問題