如何在Rails 2.3.5中从模型渲染局部 [英] How to render a Partial from a Model in Rails 2.3.5

查看:71
本文介绍了如何在Rails 2.3.5中从模型渲染局部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails 2.3.5应用程序,我试图从模型中渲染几个Partial(我知道,我知道-我不应该这样做).这样做的原因是将Comet服务器(APE)集成到我的Rails应用程序中,并且需要根据模型的事件(例如after_create)推送更新.

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).

我尝试这样做:

ActionView::Base.new(Rails::Configuration.new.view_path).render(:partial  => "pages/show", :locals => {:page => self})

这允许我呈现不使用用户帮助程序的简单部分,但是如果我尝试在我的部分中使用link_to,则会收到错误消息:

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

我确保传递给"project_path(project)"的对象不是nil.我也尝试过包括:

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?

谢谢

推荐答案

我们使用 render_anywhere gem和对此感到满意.

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

自述文件:

require 'render_anywhere'

class AnyClass
  include RenderAnwhere

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

这篇关于如何在Rails 2.3.5中从模型渲染局部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆