calling a partial from within a model / model''s method?

Joel Oliveira
Mon, 24 Nov 2008 02:31:29 -0000

Hey everyone,

I have a model I extended w/STI to allow me to write little widgets or
modules for my web-app ... that part''s working well.   However,
I''m getting
to the point where, depending on the model''s type/class I would like to
render a partial so that each type has it''s own look/feel/content.

I found this :
http://www.compulsivoco.com/2008/10/rendering-rails-partials-in-a-model-or-background-task/..
and it works pretty well.  I had all my models calling their own
partials, but, helper functions like link_to and url_for, etc, will not
work.  As mentioned in the comments to that blog post

> Love the tip, *it breaks though when your view code uses helper methods*,
> to do this, I stole from Rails internal private method,
> initialize_template_class:
>
> template_instance >
ActionView::Base.new(Rails::Configuration.new.view_path)
> template_instance.extend ApplicationController.master_helper_module
> content = template_instance.render(:partial =>
self.view_path(self.profile)
> + ''/show'', :locals => {:item => self})
>
I tried this, but am still not having any luck.

Has anyone tried this sort of thing before?  I''m trying to do a simple
link_to  for a nested resource (which works fine within the context of a
straight view, but I get this when calling the partial from my model(s)

You have a nil object when you didn''t expect it!
The error occurred while evaluating nil.url_for

Thanks everyone!