Forum: Ruby on Rails meta helper help

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Cd804cfeeea508f6bf2f5487c9ef4aed?d=identicon&s=25 Jonathan Linowes (Guest)
on 2007-06-01 08:18
(Received via mailing list)
I'd like to write a helper thats used like this:

<%= template_info %>

which replaces code like this:

<% if RAILS_ENV == "development" %>
<div class="template_info">
   Template: <%= __FILE__ %>
<% end %>

but when i write my helper it outputs the name of the helper file,
not the template! Or just a string when I try to enclose it in <% %>
How do i write a helper to output erb code thats then rendered as
part of the calling template? is that possible?

   def template_info
     if RAILS_ENV == "development"
       #"<%= __FILE__ %>"
       #content_tag("div", "<%= __FILE__ %>", :class =>
"template_info" )
Cd804cfeeea508f6bf2f5487c9ef4aed?d=identicon&s=25 linoj (Guest)
on 2007-06-02 03:40
(Received via mailing list)
hi, i found the answer -- method "caller"

so here's a neat little helper you also might like

Now the question is, could i get render to insert this automatically
at the end of each template (perhaps embeded in <!--  --> ) and toggle
it with an environment config variable???

## helper
  def template_info( update = nil, *info )
    if RAILS_ENV == "development"
      info.insert(0, "Last update: " + update.to_s(:short)) if update
      f = caller[0]
      f = f[0,f.index(':')]
      f = f.split("/").last(2).join("/")
      info.insert(0, "Template: #{f}")
      content_tag("div", info.join("<br />"), :class =>
"template_info" )

## template examples
<%= template_info %>
<%= template_info @page.updated_at %>
<%= template_info @page.updated_at, "by #{}" %>

## example html result
<div class="template_info">Template: pages/show.html.erb<br />Last
update: 31 May 16:31</div>
This topic is locked and can not be replied to.