# Testing for the existence of templates

**URL:** <https://www.ruby-forum.com/t/testing-for-the-existence-of-templates/122767>\
**Category:** Rails\
**Created:** [December 11, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/testing-for-the-existence-of-templates/122767 "2007-12-11T23:00:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Justin\_Wienckowski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d78d45/32.png) [@Justin\_Wienckowski](https://www.ruby-forum.com/u/Justin_Wienckowski)\
**Post date:** [December 11, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/testing-for-the-existence-of-templates/122767/1 "2007-12-11T23:00:13Z")

</div>

Hey folks,

I’ve run into the need to test for the existence of template files in  
several Rails apps I’ve worked on. Particularly in circumstances  
where I’m rendering a partial, and use a different partial template  
for different sub-classes of a model object. When doing so, I often  
test to make sure the template file exists first, and simply skip  
that item if no template exists.

So right now I rely on a cludgy and annoying:

File.exist?( File.join( ActionController::Base.template\_root,  
“layouts”, “some\_subdir”, “\_%s.rhtml” % method\_param ))

Anyone have a better, more elegant (and less hackish) method of doing  
this?

-Justin Wienckowski  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Justin\_Wienckowski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d78d45/32.png) [@Justin\_Wienckowski](https://www.ruby-forum.com/u/Justin_Wienckowski)\
**Post date:** [December 12, 2007, 12:24am UTC](https://www.ruby-forum.com/t/testing-for-the-existence-of-templates/122767/2 "2007-12-12T00:24:11Z")

</div>

On Dec 11, 2007, at 1:41 PM, Justin Wienckowski wrote:

> “layouts”, “some\_subdir”, “\_%s.rhtml” % method\_param ))
> 
> Anyone have a better, more elegant (and less hackish) method of doing  
> this?

I discuss some techniques for this in  
[http://www.railsdev.ws/blog/3/modular-page-assembly-in-rails/](http://www.railsdev.ws/blog/3/modular-page-assembly-in-rails/)

You might consider using request.controller and request.action, and/  
or creating a hash of path names so you can centralize @path\_names  
[‘x’] vs hard coding those directories.

–  
def gw  
acts\_as\_n00b  
writes\_at(www.railsdev.ws)  
end
