2013-06-20 38 views

回答

4

__FILE__會給你的文件名

<% __FILE__.split("/").last.sub(/^_/, "") %> 
+1

謝謝,我知道關於'__FILE__'變量。我希望有一些像'partial_counter'這樣的本地特色。 – Aaron

2

在你的部分:

<%= partial_class(__FILE__) %> 

在application_helper:

def partial_class(partial) 
     partial.split(".").first.split("/").last.sub(/^_/, "") 
    end 

結果:partial爲'_customer-existing.html.erb',輸出爲'customer-existing'。我經常在部分內部的包裝div上使用這個名稱,以便我可以在jquery中使用相同的名稱來顯示/隱藏部分。

實施例:

<div class='<%= partial_class(__FILE__) %>'> 
    stuff here that will be show/hideable by partial name. 
</div>