未定义的方法“url_for”在 ActiveRecord 模型中渲染 Rails 2.3.x 模板时
我知道这不是“可接受的”做法,但作为后台进程,我将渲染的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有一个旧应用程序,我必须在模型中执行类似的操作(但使用
polymorphic_path
和polymorphic_url
)。以下包含让我可以访问
url_for
和polymorphic_path
等。I have an old app that I had to do something similar (but made use of
polymorphic_path
andpolymorphic_url
) in a model.The following includes gave me access to
url_for
andpolymorphic_path
etc.