gpt4 book ai didi

Can you get a Rails partial's name from within the partial?(您能从Partial中获取Rails Partial的名称吗?)

转载 作者:bug小助手 更新时间:2023-10-25 15:07:54 26 4
gpt4 key购买 nike



Is there a variable that contains a partial's name accessible from within the partial?

是否存在包含可从Partial中访问的Partial名称的变量?



render partial: 'foo'


In _foo.haml:

在_foo.haml中:



.name
= partial_name # would output "foo"

更多回答
优秀答案推荐

__FILE__ will give you the file name

__FILE__将为您提供文件名



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


In your partial:

在你的部分中:



    <%= partial_class(__FILE__) %>


In application_helper:

在应用程序帮助程序中(_H):



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


Result: partial is '_customer-existing.html.erb', output is 'customer-existing'. I use this constantly for class names on a wrapper div inside the partial, so that I can use the same name in jquery to show/hide the partial.

结果:部分为‘_Customer-Existing.html.erb’,输出为‘Customer-Existing’。我经常在Partial内的包装器div上使用它作为类名,这样我就可以在jQuery中使用相同的名称来显示/隐藏Partial。



Example:

示例:



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


You can use a helper in application_helper for this and use rubys introspection abilities
so you dont have to explicitly pass FILE
like in Mikes answer.

为此,您可以使用APPLICATION_HELPER中的帮助器,并使用Rubys自省功能,这样您就不必像在Mikes Answer中那样显式地传递文件。


def foo                                                                                                                 
caller_locations(1, 1).first.path
end

更多回答

Thanks, I knew about the __FILE__ variable. I was hoping there was something local like the partial_counter.

谢谢,我知道__FILE__变量。我希望有像部分柜台这样的地方。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com