是否有Rails函数来检查部分是否存在?

时间:2021-04-21 20:12:08

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:

当我呈现一个不存在的部分时,我得到一个异常。我想在渲染之前检查一个局部是否存在,如果它不存在,我将渲染其他的东西。我在我的。erb文件中做了如下代码,但是我认为应该有更好的方法:

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

7 个解决方案

#1


91  

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

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

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.

我所见过的其他解决方案的优点是,它将在所有视图路径中查找,而不仅仅是您的rails根。这对我来说很重要,因为我有很多rails引擎。

This also works in Rails 4.

这也适用于Rails 4。

#2


67  

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?

编辑1:哦,我的阅读理解不及格。你说过你想渲染别的东西。那样的话,这个怎么样?

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

or

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

Edit 2:

编辑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")) %>

#3


49  

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)

从视图内部来看,template_exists?可以,但是调用约定不能使用单个部分名称字符串,而是使用template_exists?(名称、前缀部分)

To check for partial on path: app/views/posts/_form.html.slim

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

Use:

使用:

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

#4


30  

In Rails 3.2.13, if you're in a controller, you can use this :

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

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

template_exists? is delegated to lookupcontext, as you can see in AbstractController::ViewPaths

template_exists吗?被委托给lookupcontext,您可以在AbstractController:::ViewPaths中看到

_prefixes gives the context of the controller's inheritance chain.

_prefixes给出控制器继承链的上下文。

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

http://api.rubyonrails.org/classes/ActionView/LookupContext/ViewPaths.html method-i-template_exists-3F

#5


8  

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

Rails 4.2

First, i put this in my application_helper.rb

首先,我把这个放到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? %>

<%=呈现"#{dynamic_path}" if lookup_context.find_all("#{dynamic_path}",[],true).有吗?% >

i just call <%= render_if_exists "#{dynamic_path}" %>

我只调用<%= render_if_exists "#{dynamic_path}" %>

hope that helps. (haven't tried in rails3)

希望有帮助。(在rails3没有试过)

#6


5  

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.

如果我们只是使用代码<%=呈现:partial => "# dynamic_partial}"来拯救nil %>或某些派生,那么这个部分可能存在,但会引发一个异常,这个异常将被悄悄地吃掉,并成为调试的痛苦来源。

#7


3  

What about your own helper:

你自己的帮手呢?

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

#1


91  

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

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

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.

我所见过的其他解决方案的优点是,它将在所有视图路径中查找,而不仅仅是您的rails根。这对我来说很重要,因为我有很多rails引擎。

This also works in Rails 4.

这也适用于Rails 4。

#2


67  

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?

编辑1:哦,我的阅读理解不及格。你说过你想渲染别的东西。那样的话,这个怎么样?

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

or

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

Edit 2:

编辑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")) %>

#3


49  

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)

从视图内部来看,template_exists?可以,但是调用约定不能使用单个部分名称字符串,而是使用template_exists?(名称、前缀部分)

To check for partial on path: app/views/posts/_form.html.slim

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

Use:

使用:

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

#4


30  

In Rails 3.2.13, if you're in a controller, you can use this :

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

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

template_exists? is delegated to lookupcontext, as you can see in AbstractController::ViewPaths

template_exists吗?被委托给lookupcontext,您可以在AbstractController:::ViewPaths中看到

_prefixes gives the context of the controller's inheritance chain.

_prefixes给出控制器继承链的上下文。

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

http://api.rubyonrails.org/classes/ActionView/LookupContext/ViewPaths.html method-i-template_exists-3F

#5


8  

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

Rails 4.2

First, i put this in my application_helper.rb

首先,我把这个放到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? %>

<%=呈现"#{dynamic_path}" if lookup_context.find_all("#{dynamic_path}",[],true).有吗?% >

i just call <%= render_if_exists "#{dynamic_path}" %>

我只调用<%= render_if_exists "#{dynamic_path}" %>

hope that helps. (haven't tried in rails3)

希望有帮助。(在rails3没有试过)

#6


5  

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.

如果我们只是使用代码<%=呈现:partial => "# dynamic_partial}"来拯救nil %>或某些派生,那么这个部分可能存在,但会引发一个异常,这个异常将被悄悄地吃掉,并成为调试的痛苦来源。

#7


3  

What about your own helper:

你自己的帮手呢?

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