2
是否有一個變量包含可以從部分內部訪問的部分名稱?你能從部分內部得到一個Rails的部分名稱嗎?
render :partial => 'foo'
在_foo.haml:
.name
= partial_name # would output "foo"
是否有一個變量包含可以從部分內部訪問的部分名稱?你能從部分內部得到一個Rails的部分名稱嗎?
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
結果: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>
謝謝,我知道關於'__FILE__'變量。我希望有一些像'partial_counter'這樣的本地特色。 – Aaron