2011-11-09 92 views
2

我知道這不是一種「可接受」的做法,但作爲後臺進程,我將渲染的Rails部分緩存到數據庫中,以便通過JSONP 。我發現了這個主題的許多資源,併成功地在ActiveRecord模型中輸出Rails 2.3.x的部分內容,但是如果我在部分Rails中使用動態URL,則會爆發。在ActiveRecord模型中渲染Rails 2.3.x模板時未定義的方法'url_for'

我得到一個成功的輸出(減去動態URL)這...

def self.compile_html 

    viewer = Class.new(ApplicationController) 
    path = ActionController::Base.view_paths rescue ActionController::Base.view_root 

    Question.all.each do |q| 
    q.html = ActionView::Base.new(path, {}, viewer).render(:partial => "questions/cached_output", :locals => { :question => q }) 
    sleep 1 # hold for complete output and debugging only 
    q.save(false) # bypass validations 
    end 
    return "Caching complete" 
end 

我都試過,包括ActionController::UrlWriter模塊和ActionView::Helpers::UrlHelper,沒有運氣。我似乎無法找到實例化/包含順序,以允許部分訪問ActionController::Base.new.url_for()將動態路由寫入部分。

我沒有嘗試類似的過程作爲RoR: undefined method `url_for' for nil:NilClass沒有運氣

我知道render_anywhere是爲Rails應用程序3.x的一個解決方案,但我對Rails 2.3.11

思想工作?

回答

2

我有一箇舊的應用程序,我不得不在模型中做類似的事情(但是使用了polymorphic_pathpolymorphic_url)。

以下包括給我訪問url_forpolymorphic_path

include ActionView::Helpers::UrlHelper 
include ActionView::Helpers::TagHelper 
include ActionController::PolymorphicRoutes 
include ActionController::UrlWriter 
+0

是由包括::的ActionView Base或ActionController的::基地? – ACuppy

+0

我在我的模型中使用了這些文件,它不在視圖或控制器內部 - 它在內部擴展了ActiveRecord :: Base –