簡體   English   中英

您可以從部分中獲取 Rails 部分的名稱嗎?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM