如何在 Rails 2.3.5 中从模型渲染部分

发布于 2024-09-05 01:35:05 字数 666 浏览 6 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(2

黎歌 2024-09-12 01:35:08

我们使用 render_anywhere gem 并且对此感到满意。

来自自述文件:

require 'render_anywhere'

class AnyClass
  include RenderAnwhere

  def build_html
    html = render :template => 'normal/template/reference',
                  :layout => 'application'
    html
  end
end

We use the render_anywhere gem and have been happy with it.

From the README:

require 'render_anywhere'

class AnyClass
  include RenderAnwhere

  def build_html
    html = render :template => 'normal/template/reference',
                  :layout => 'application'
    html
  end
end
坠似风落 2024-09-12 01:35:08

包括这两个模块就足够了。也许您忘记设置 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.

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