开发者

In Rails, how to write a helper function "who_am_i" so that a view or partial can use it to report which file it is?

开发者 https://www.devze.com 2023-02-18 15:53 出处:网络
When designing for layout and styling with CSS, it might be useful to add a line in the view or partial:

When designing for layout and styling with CSS, it might be useful to add a line in the view or partial:

<%= who_am_i %>

or if using haml:

= who_am_i

so that it will print on th开发者_StackOverflowe webpage

this is view/products/_footer.html.haml

How can this helper be written? (or is there one already in Rails?)

(my first try was to use __FILE__ and do some manipulation with string, and it works well if everything is done inside the view or partial file, but when it moves to helper, then __FILE__ becomes helpers/application_helper.rb so it won't work. But I'd like to find out possibly better ways to do it)


Your helper method should be:

def who_am_i
  @template.template
end

Rails 3

def who_am_i
  @_virtual_path
end

Note:

I tested the solution in Rails 3.0.5 and it works. I am not certain that it will work in all scenarios. Use with caution.

0

精彩评论

暂无评论...
验证码 换一张
取 消