2010-08-30 78 views
1

假設我將路由/連接到WelcomeController的索引操作。Rails 3中的模板路徑

index.html.erb-Template內部我想從Rails.root向上顯示模板的路徑,即。

<h1> We are rendering: <%= how_do_i_do_this? %></h1> 

渲染到

<h1> We are rendering: app/views/presentation/index.html.erb</h1> 

在Rails 2我可以訪問template.path,但是這並不能工作了

任何想法?

回答

0

由於在Rails中模板呈現的工作原理,您現在可以使用__FILE__來代替它。這適用於我:

<%= __FILE__.gsub(Rails.root.to_s, "") %> 

但是,可能有更好的方法來做到這一點,但我找不到它,當我去找。

+0

是的,這沒有工作。然而,它帶來的主要缺點是你不能將它分解成一個輔助方法,因爲這會搞砸__FILE__常量。 – flitzwald 2010-08-30 13:49:55

0

瑞恩的答案有效。如果你也想把你的方法放在幫手裏,使用Kernel#caller。這裏是我用來做類似的一種方法:

def has_page_comment? code = nil 
    if code.nil? 
    # grab caller file, sanitize 
    code = caller.first.split(':').first.gsub(Rails.root.to_s,'').gsub('.html.erb','') 
    end 
    ... 
end