【问题标题】:Undefined method 'url_for' while rendering a Rails 2.3.x template inside a ActiveRecord model在 ActiveRecord 模型中渲染 Rails 2.3.x 模板时未定义的方法“url_for”
【发布时间】:2011-11-09 19:31:21
【问题描述】:

我知道这不是“可接受的”做法,但作为后台进程,我将渲染的 Rails 部分缓存到数据库中,以便通过 JSONP 更快地输出。我在该主题上找到了许多资源,并在 ActiveRecord 模型中成功输出了 Rails 2.3.x 部分,但是如果我在部分 Rails 中使用动态 URL 就会爆炸。

我得到一个成功的输出(减去动态 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

想法?

【问题讨论】:

    标签: ruby-on-rails actioncontroller actionviewhelper


    【解决方案1】:

    我有一个旧应用程序,我必须在模型中执行类似的操作(但使用了 polymorphic_path 和 polymorphic_url)。

    以下内容让我可以访问url_for 和polymorphic_path 等。

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

    【讨论】:

    • 是被 ActionView::Base 还是 ActionController::Base 包含的?
    • 我在我的模型中使用了这些文件,它不在视图或控制器内——它在扩展 ActiveRecord::Base 的东西内
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多