Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a Partial from a Model in Rails 2.3.5

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

like image 837
empire29 Avatar asked Jun 09 '10 05:06

empire29


People also ask

What is render partial in Ruby on Rails?

Rails Guides describes partials this way: Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.

What is the difference between render and Redirect_to in Rails?

There is an important difference between render and redirect_to: render will tell Rails what view it should use (with the same parameters you may have already sent) but redirect_to sends a new request to the browser.

How can you tell Rails to render without a layout?

2.2. By default, if you use the :plain option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the layout: true option and use the . text. erb extension for the layout file.


1 Answers

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
like image 86
klochner Avatar answered Nov 01 '22 13:11

klochner