How to render a Partial from a Model in Rails 2.3.

2019-05-01 04:31发布

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

2条回答
成全新的幸福
2楼-- · 2019-05-01 05:04

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.

查看更多
Fickle 薄情
3楼-- · 2019-05-01 05:19

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
查看更多
登录 后发表回答