views:

813

answers:

3

Is there some way to determine what file currently is being rendered by Rails (2.2) in a helper method. An example result would be "/sessions/new.html.erb" or something similar.

I am trying to write a helper function that does something based on the file name that is being rendered, so I need a reliable way to obtain this information. Is there an official way to get this information?

+1  A: 

I now have this extremely ugly solution. Anybody a better idea?


begin
  raise "Nasty hack"
rescue 
  render = [email protected] { |bti| /\/app\/views\/(.+)\:\d+\:in \`_run_erb_/ =~ bti }
  return $1
end
wvanbergen
+1  A: 

In Rails 2.2 you can use @template.template

roninek
A: 

Thanks, that did it!

@template.template.relative_path will return the path of the file being rendered.

wvanbergen