# Calling a partial from within a model / model's method?

**URL:** <https://www.ruby-forum.com/t/calling-a-partial-from-within-a-model-models-method/153966>\
**Category:** Rails\
**Created:** [November 24, 2008, 3:32am UTC](https://www.ruby-forum.com/t/calling-a-partial-from-within-a-model-models-method/153966 "2008-11-24T03:32:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joel\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@Joel\_O](https://www.ruby-forum.com/u/Joel_O)\
**Post date:** [November 24, 2008, 3:32am UTC](https://www.ruby-forum.com/t/calling-a-partial-from-within-a-model-models-method/153966/1 "2008-11-24T03:32:20Z")

</div>

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/](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!

- Joel

---

<div class="post-metadata">

**Author:** ![Joel\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@Joel\_O](https://www.ruby-forum.com/u/Joel_O)\
**Post date:** [November 24, 2008, 3:39am UTC](https://www.ruby-forum.com/t/calling-a-partial-from-within-a-model-models-method/153966/2 "2008-11-24T03:39:21Z")

</div>

On Nov 23, 9:31 pm, “Joel O.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> work. As mentioned in the comments to that blog post
> 
> - Joel

Hi Joel. I’m not quite sure why you want to render partials or  
templates from within your model(s). I’m sure you have some good  
reasons, but sometimes there’re ways of accomplishing what you want  
without violating the “rules” of the MVC design pattern.

Would you mind explaining in more detail why your model(s) need to  
render partials and/or templates?

Cheers,  
Nick

---

<div class="post-metadata">

**Author:** ![Joel\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@Joel\_O](https://www.ruby-forum.com/u/Joel_O)\
**Post date:** [November 24, 2008, 2:38pm UTC](https://www.ruby-forum.com/t/calling-a-partial-from-within-a-model-models-method/153966/3 "2008-11-24T14:38:13Z")

</div>

Hey Nick!

First - thanks for the reply.

Second - the reason why I’m trying to do this is to allow whatever type  
of  
widget model I’m creating to render its own particular partial, in the  
context of the “area” a user will place it - header, footer, sidebar,  
etc.  
I thought that calling a helper to weed out the objects for a particular  
“area” and then letting the model call its partial on its own, would be  
the  
easier way to do it, despite the normal MVC rules. I know it’s against  
the  
rules - so I’m wondering if there’s an easier way?

Any input would be appreciated! 🙂

Thanks!

- Joel

---

<div class="post-metadata">

**Author:** ![Joel\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@Joel\_O](https://www.ruby-forum.com/u/Joel_O)\
**Post date:** [December 12, 2008, 3:21am UTC](https://www.ruby-forum.com/t/calling-a-partial-from-within-a-model-models-method/153966/4 "2008-12-12T03:21:59Z")

</div>

Weeks later I finally got around to figuring out how to best tackle this  
and  
your suggestion was right on the money. I didn’t want to let this lie  
without mentioning that the best bet started with that nugget from the  
pastie link. Thanks Nick 🙂

- Joel

---

<div class="post-metadata">

**Author:** ![Joel\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@Joel\_O](https://www.ruby-forum.com/u/Joel_O)\
**Post date:** [November 24, 2008, 5:42pm UTC](https://www.ruby-forum.com/t/calling-a-partial-from-within-a-model-models-method/153966/5 "2008-11-24T17:42:14Z")

</div>

On 2008-11-24, at 08:37, Joel O. wrote:

> the normal MVC rules. I know it’s against the rules - so I’m  
> wondering if there’s an easier way?
> 
> Any input would be appreciated! 🙂
> 
> Thanks!
> 
> - Joel

Hi Joel. Why not create a helper method that determines which partial  
should be rendered? For example:  
[http://pastie.org/pastes/322615](http://pastie.org/pastes/322615)

-Nick

---

<div class="post-metadata">

**Author:** ![Joel\_O](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@Joel\_O](https://www.ruby-forum.com/u/Joel_O)\
**Post date:** [December 15, 2008, 7:16pm UTC](https://www.ruby-forum.com/t/calling-a-partial-from-within-a-model-models-method/153966/6 "2008-12-15T19:16:27Z")

</div>

On 2008-12-11, at 21:21, Joel O. wrote:

> Weeks later I finally got around to figuring out how to best tackle  
> this and your suggestion was right on the money. I didn’t want to  
> let this lie without mentioning that the best bet started with that  
> nugget from the pastie link. Thanks Nick 🙂
> 
> - Joel

I’m glad to hear it helped! Good luck, mate.  
-Nick
