2013-06-20 36 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>