Ruby-on-rails 是否有任何 Rails 函数来检查部分是否存在?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3559419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:06:02  来源:igfitidea点击:

Is there any Rails function to check if a partial exists?

ruby-on-railspartial

提问by Daniel Cukier

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 %>

回答by Rein

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。

回答by Jeff

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

回答by Luke Imhoff

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?(name, prefix, partial)

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

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

Use:

用:

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

回答by Flackou

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

_prefixesgives the context of the controller's inheritance chain.

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

truebecause you're looking for a partial (you can omit this argument if you want a regular template).

true因为你正在寻找一个部分(如果你想要一个常规模板,你可以省略这个参数)。

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

回答by afxjzs

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

导轨 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? %>

<%= render "#{dynamic_path}" if lookup_context.find_all("#{dynamic_path}",[],true).any? %>

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

我只是打电话 <%= render_if_exists "#{dynamic_path}" %>

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

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

回答by br3nt

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.

如果我们只是使用代码<%= render :partial => "#{dynamic_partial}" rescue nil %>或一些派生类,部分可能存在但会引发异常,该异常将被默默吃掉并成为调试的痛苦之源。

回答by Andrey

What about your own helper:

你自己的帮手呢:

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