Rails 是否有任何函数可以检查部分是否存在?
当我渲染不存在的部分时,我收到异常。我想在渲染之前检查部分是否存在,如果不存在,我将渲染其他内容。我在 .erb 文件中执行了以下代码,但我认为应该有更好的方法来执行此操作:
<% begin %>
<%= render :partial => "#{dynamic_partial}" %>
<% rescue ActionView::MissingTemplate %>
Can't show this data!
<% end %>
When I render a partial which does not exists, I get an Exception. I'd like to check if a partial exists before rendering it and in case it doesn't exist, I'll render something else. I did the following code in my .erb file, but I think there should be a better way to do this:
<% begin %>
<%= render :partial => "#{dynamic_partial}" %>
<% rescue ActionView::MissingTemplate %>
Can't show this data!
<% end %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
目前,我在 Rails 3/3.1 项目中使用以下内容:
与我见过的其他解决方案相比,它的优点是它会在所有视图路径中查找,而不仅仅是在 Rails 根目录中查找。这对我来说很重要,因为我有很多铁路引擎。
这也适用于 Rails 4。
Currently, I'm using the following in my Rails 3/3.1 projects:
The advantage over other solutions I've seen is that this will look in all view paths instead of just your rails root. This is important to me as I have a lot of rails engines.
This also works in Rails 4.
我也为此苦苦挣扎。这是我最终使用的方法:
基本上,如果部分不存在,则不执行任何操作。如果部分内容丢失,您是否想打印一些内容?
编辑1:哦,我阅读理解不及格。你确实说过你想渲染其他东西。既然如此,那这个又如何呢?
或
编辑2:
替代方案:检查部分文件是否存在:
I was struggling with this too. This is the method I ended up using:
Basically, if the partial doesn't exist, do nothing. Did you want to print something if the partial is missing, though?
Edit 1: Oh, I fail at reading comprehension. You did say that you wanted to render something else. In that case, how about this?
or
Edit 2:
Alternative: Checking for existence of the partial file:
从视图内部看,template_exists?有效,但调用约定不适用于单个部分名称字符串,而是需要 template_exists?(name, prefix,partial)
要检查路径上的部分:
app/views/posts/_form.html.slim
使用:
From inside a view, template_exists? works, but the calling convention doesn't work with the single partial name string, instead it takes template_exists?(name, prefix, partial)
To check for partial on path:
app/views/posts/_form.html.slim
Use:
在 Rails 3.2.13 中,如果您在控制器中,则可以使用:
template_exists?
被委托给lookupcontext
,如AbstractController::ViewPaths
< /a>_prefixes
给出控制器继承链的上下文。true
因为您正在寻找部分模板(如果您想要常规模板,可以省略此参数)。http://api.rubyonrails.org/classes /ActionView/LookupContext/ViewPaths.html#method-i-template_exists-3F
In Rails 3.2.13, if you're in a controller, you can use this :
template_exists?
is delegated tolookupcontext
, as you can see inAbstractController::ViewPaths
_prefixes
gives the context of the controller's inheritance chain.true
because you're looking for a partial (you can omit this argument if you want a regular template).http://api.rubyonrails.org/classes/ActionView/LookupContext/ViewPaths.html#method-i-template_exists-3F
我知道这个问题已经得到解答并且已有一百万年的历史了,但是这就是我最终为我修复这个问题的方法...
Rails 4.2
首先,我将其放入我的 application_helper.rb 中
,现在而不是调用
<%=如果lookup_context.find_all("#{dynamic_path}",[],true).any,则渲染“#{dynamic_path}”? %>
我只是调用
<%= render_if_exists "#{dynamic_path}" %>
希望有帮助。 (没有在rails3中尝试过)
I know this has been answered and is a million years old, but here's how i ended up fixing this for me...
Rails 4.2
First, i put this in my application_helper.rb
and now instead of calling
<%= render "#{dynamic_path}" if lookup_context.find_all("#{dynamic_path}",[],true).any? %>
i just call
<%= render_if_exists "#{dynamic_path}" %>
hope that helps. (haven't tried in rails3)
我在很多场合使用了这个范例,并取得了巨大的成功:
上面代码的好处是我们可以处理两种特定情况:
如果我们只使用代码
<%= 渲染:部分 => "#{dynamic_partial}"rescue nil %>
或某些派生类,部分可能存在,但会引发异常,该异常将被默默地吞噬并成为调试的痛苦来源。I have used this paradigm on many occasions with great success:
The benefit of the code above is that we can handle tow specific cases:
If we just use the code
<%= render :partial => "#{dynamic_partial}" rescue nil %>
or some derivative, the partial may exist but raise an exception which will be silently eaten and become a source of pain to debug.你自己的帮手呢:
What about your own helper:
这在 Rails 6.1 中适用于我:
这里我的部分嵌套比正常更深一些级别(
app/views/path/after/app/views/_override_partial
),所以这就是为什么我将其添加为prefixes 数组,但如果不需要,可以使用lookup_context.prefixes
代替。我还可以在控制器上使用
prepend_view_path
。由你决定 :)This works for me in Rails 6.1:
Here I have my partial nested some levels deeper than normal (
app/views/path/after/app/views/_override_partial
) so that's why I'm adding it as the prefixes array, but you can uselookup_context.prefixes
instead if you don't need it.I could have also used
prepend_view_path
on the controller. It's up to you :)