未定义的方法“url_for”在 ActiveRecord 模型中渲染 Rails 2.3.x 模板时

发布于 2024-12-14 13:15:10 字数 1112 浏览 0 评论 0原文

我知道这不是“可接受的”做法,但作为后台进程,我将渲染的 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 工作

有想法吗?

I know that this not an "acceptable" practice but as a background process I am caching a rendered Rails partial into the database for faster output via JSONP. I have found many resources on the topic and successfully output a Rails 2.3.x partial within a ActiveRecord model, BUT if I use dynamic URLs within a partial Rails blows up.

I get a successful output (minus the dynamic URLs) with this...

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

I have tried including ActionController::UrlWriter Modules and ActionView::Helpers::UrlHelper with no luck. I cannot seems to find the instantiation/inclusion order to allow the partial to access ActionController::Base.new.url_for() to write the dynamic routes to the partial.

I did attempt a similar process as RoR: undefined method `url_for' for nil:NilClass without luck

I know that render_anywhere is a solution for Rails 3.x apps, but I am working in Rails 2.3.11

Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是偏爱你 2024-12-21 13:15:10

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

以下包含让我可以访问 url_forpolymorphic_path 等。

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

I have an old app that I had to do something similar (but made use of polymorphic_path and polymorphic_url) in a model.

The following includes gave me access to url_for and polymorphic_path etc.

include ActionView::Helpers::UrlHelper
include ActionView::Helpers::TagHelper
include ActionController::PolymorphicRoutes
include ActionController::UrlWriter
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文