Rails 是否有任何函数可以检查部分是否存在?

发布于 2024-09-16 00:00:34 字数 303 浏览 3 评论 0原文

当我渲染不存在的部分时,我收到异常。我想在渲染之前检查部分是否存在,如果不存在,我将渲染其他内容。我在 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

血之狂魔 2024-09-23 00:00:34

目前,我在 Rails 3/3.1 项目中使用以下内容:

lookup_context.find_all('posts/_form').any?

与我见过的其他解决方案相比,它的优点是它会在所有视图路径中查找,而不仅仅是在 Rails 根目录中查找。这对我来说很重要,因为我有很多铁路引擎。

这也适用于 Rails 4。

Currently, I'm using the following in my Rails 3/3.1 projects:

lookup_context.find_all('posts/_form').any?

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.

猫卆 2024-09-23 00:00:34

我也为此苦苦挣扎。这是我最终使用的方法:

<%= render :partial => "#{dynamic_partial}" rescue nil %>

基本上,如果部分不存在,则不执行任何操作。如果部分内容丢失,您是否想打印一些内容?

编辑1:哦,我阅读理解不及格。你确实说过你想渲染其他东西。既然如此,那这个又如何呢?

<%= render :partial => "#{dynamic_partial}" rescue render :partial => 'partial_that_actually_exists' %>

<%= render :partial => "#{dynamic_partial}" rescue "Can't show this data!" %>

编辑2:

替代方案:检查部分文件是否存在:

<%= render :partial => "#{dynamic_partial}" if File.exists?(Rails.root.join("app", "views", params[:controller], "_#{dynamic_partial}.html.erb")) %>

I was struggling with this too. This is the method I ended up using:

<%= render :partial => "#{dynamic_partial}" rescue nil %>

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?

<%= render :partial => "#{dynamic_partial}" rescue render :partial => 'partial_that_actually_exists' %>

or

<%= render :partial => "#{dynamic_partial}" rescue "Can't show this data!" %>

Edit 2:

Alternative: Checking for existence of the partial file:

<%= render :partial => "#{dynamic_partial}" if File.exists?(Rails.root.join("app", "views", params[:controller], "_#{dynamic_partial}.html.erb")) %>
ま昔日黯然 2024-09-23 00:00:34

从视图内部看,template_exists?有效,但调用约定不适用于单个部分名称字符串,而是需要 template_exists?(name, prefix,partial)

要检查路径上的部分:
app/views/posts/_form.html.slim

使用:

lookup_context.template_exists?("form", "posts", true)

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:

lookup_context.template_exists?("form", "posts", true)
清晰传感 2024-09-23 00:00:34

在 Rails 3.2.13 中,如果您在控制器中,则可以使用:

template_exists?("#{dynamic_partial}", _prefixes, true)

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?("#{dynamic_partial}", _prefixes, true)

template_exists? is delegated to lookupcontext, as you can see in AbstractController::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

(り薆情海 2024-09-23 00:00:34

我知道这个问题已经得到解答并且已有一百万年的历史了,但是这就是我最终为我修复这个问题的方法...

Rails 4.2

首先,我将其放入我的 application_helper.rb 中

  def render_if_exists(path_to_partial)
    render path_to_partial if lookup_context.find_all(path_to_partial,[],true).any?
  end

,现在而不是调用

<%=如果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

  def render_if_exists(path_to_partial)
    render path_to_partial if lookup_context.find_all(path_to_partial,[],true).any?
  end

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)

前事休说 2024-09-23 00:00:34

我在很多场合使用了这个范例,并取得了巨大的成功:

<%=
  begin
    render partial: "#{dynamic_partial}"
  rescue ActionView::MissingTemplate
    # handle the specific case of the partial being missing
  rescue
    # handle any other exception raised while rendering the partial
  end
%>

上面代码的好处是我们可以处理两种特定情况:

  • 部分确实丢失
  • 部分存在,但由于某种原因引发了错误

如果我们只使用代码<%= 渲染:部分 => "#{dynamic_partial}"rescue nil %> 或某些派生类,部分可能存在,但会引发异常,该异常将被默默地吞噬并成为调试的痛苦来源。

I have used this paradigm on many occasions with great success:

<%=
  begin
    render partial: "#{dynamic_partial}"
  rescue ActionView::MissingTemplate
    # handle the specific case of the partial being missing
  rescue
    # handle any other exception raised while rendering the partial
  end
%>

The benefit of the code above is that we can handle tow specific cases:

  • The partial is indeed missing
  • The partial exists, but it threw an error for some reason

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.

是你 2024-09-23 00:00:34

你自己的帮手呢:

def render_if_exists(path, *args)
  render path, *args
rescue ActionView::MissingTemplate
  nil
end

What about your own helper:

def render_if_exists(path, *args)
  render path, *args
rescue ActionView::MissingTemplate
  nil
end
绻影浮沉 2024-09-23 00:00:34

这在 Rails 6.1 中适用于我:

<% if lookup_context.exists?("override_partial", ['path/after/app/views'], true) %>
  <%= render partial: "path/after/app/views/override_partial" %>
<% else %>
  <%= render partial: "default_partial" %>
<% end %>

这里我的部分嵌套比正常更深一些级别(app/views/path/after/app/views/_override_partial),所以这就是为什么我将其添加为prefixes 数组,但如果不需要,可以使用 lookup_context.prefixes 代替。

我还可以在控制器上使用 prepend_view_path 。由你决定 :)

This works for me in Rails 6.1:

<% if lookup_context.exists?("override_partial", ['path/after/app/views'], true) %>
  <%= render partial: "path/after/app/views/override_partial" %>
<% else %>
  <%= render partial: "default_partial" %>
<% end %>

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 use lookup_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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文