Testing for the existence of templates

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
[email protected]

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/

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs