[英]Can you get a Rails partial's name from within the partial?
是否有一個包含可從部分內部訪問的部分名稱的變量?
render partial: 'foo'
在_foo.haml
:
.name
= partial_name # would output "foo"
__FILE__
會給你文件名
<% __FILE__.split("/").last.sub(/^_/, "") %>
在你的部分:
<%= partial_class(__FILE__) %>
在 application_helper 中:
def partial_class(partial)
partial.split(".").first.split("/").last.sub(/^_/, "")
end
結果:部分是'_customer-existing.html.erb',輸出是'customer-existing'。 我經常將它用於部分內部包裝器 div 上的類名,以便我可以在 jquery 中使用相同的名稱來顯示/隱藏部分。
例子:
<div class='<%= partial_class(__FILE__) %>'>
stuff here that will be show/hideable by partial name.
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.