如何在 Rails 2.3.5 中从模型渲染部分
我有一个 Rails 2.3.5 应用程序,我试图从模型中渲染几个部分(我知道,我知道 - 我不应该这样做)。我这样做的原因是我将 Comet 服务器(APE)集成到我的 Rails 应用程序中,并且需要根据模型的事件(例如 after_create)推送更新。
我尝试这样做:
ActionView::Base.new(Rails::Configuration.new.view_path).render(:partial => "pages/show", :locals => {:page => self})
这允许我渲染不使用帮助程序的简单部分,但是如果我尝试在我的部分中使用 link_to ,我会收到一条错误消息:
undefined method `url_for' for nil:NilClass
我已确保将对象传递到“project_path(project)”不为零。我还尝试过 include:
include ActionView::Helpers::UrlHelper
include ActionController::UrlWriter
在包含进行上述“渲染”调用的方法的模块中。
有谁知道如何解决这个问题?
谢谢
I have a Rails 2.3.5 application and Im trying to render several Partials from within a Model (i know, i know -- im not supposed to). The reason im doing this is im integrating a Comet server (APE) into my Rails app and need to push updates out based on the Model's events (ex. after_create).
I have tried doing this:
ActionView::Base.new(Rails::Configuration.new.view_path).render(:partial => "pages/show", :locals => {:page => self})
Which allows me to render simple partials that don't user helpers, however if I try to user a link_to in my partial, i receive an error stating:
undefined method `url_for' for nil:NilClass
I've made sure that the object being passed into the "project_path(project)" is not nil. I've also tried including:
include ActionView::Helpers::UrlHelper
include ActionController::UrlWriter
in the Module that contains the method that makes the above "render" call.
Does anyone know how to work around this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们使用 render_anywhere gem 并且对此感到满意。
来自自述文件:
We use the render_anywhere gem and have been happy with it.
From the README:
包括这两个模块就足够了。也许您忘记设置
default_url_options[:host]
?如果没有它,您可以使用_path
帮助程序,但不能使用_url
帮助程序。包含这些模块并检查它是否在 irb 中工作,也许它会引导您找到正确的解决方案。
Including these two modules should be enough. Maybe you forgot to set
default_url_options[:host]
? Without it you can use_path
helpers, but not_url
ones.Include these modules and check out if it works in
irb
, maybe it will lead you to right solution.