Dan*_*ier 97 ruby-on-rails partial
当我渲染一个不存在的部分时,我得到一个例外.我想在渲染之前检查是否存在部分,如果它不存在,我将渲染其他内容.我在.erb文件中执行了以下代码,但我认为应该有更好的方法来执行此操作:
<% begin %>
<%= render :partial => "#{dynamic_partial}" %>
<% rescue ActionView::MissingTemplate %>
Can't show this data!
<% end %>
Run Code Online (Sandbox Code Playgroud)
小智 97
目前,我在我的Rails 3/3.1项目中使用以下内容:
lookup_context.find_all('posts/_form').any?
Run Code Online (Sandbox Code Playgroud)
与我见过的其他解决方案相比,优势在于它将在所有视图路径中查找,而不仅仅是您的rails根目录.这对我很重要,因为我有很多rails引擎.
这也适用于Rails 4.
Jef*_*eff 69
我也在努力解决这个问题.这是我最终使用的方法:
<%= render :partial => "#{dynamic_partial}" rescue nil %>
Run Code Online (Sandbox Code Playgroud)
基本上,如果部分不存在,则什么也不做.但是,如果缺少部分内容,您是否想要打印一些内容?
编辑1:哦,我在阅读理解上失败了.你确实说过想要渲染别的东西.在那种情况下,这个怎么样?
<%= render :partial => "#{dynamic_partial}" rescue render :partial => 'partial_that_actually_exists' %>
Run Code Online (Sandbox Code Playgroud)
要么
<%= render :partial => "#{dynamic_partial}" rescue "Can't show this data!" %>
Run Code Online (Sandbox Code Playgroud)
编辑2:
备选方案:检查是否存在部分文件:
<%= render :partial => "#{dynamic_partial}" if File.exists?(Rails.root.join("app", "views", params[:controller], "_#{dynamic_partial}.html.erb")) %>
Run Code Online (Sandbox Code Playgroud)
Luk*_*off 52
从视图中,template_exists?有效,但调用约定不适用于单个部分名称字符串,而是需要template_exists?(名称,前缀,部分)
要检查部分路径:app/views/posts/_form.html.slim
使用:
lookup_context.template_exists?("form", "posts", true)
Run Code Online (Sandbox Code Playgroud)
Fla*_*kou 30
在Rails 3.2.13中,如果你在控制器中,你可以使用:
template_exists?("#{dynamic_partial}", _prefixes, true)
Run Code Online (Sandbox Code Playgroud)
template_exists?
lookupcontext
如你所见,被委派给AbstractController::ViewPaths
_prefixes
给出了控制器继承链的上下文.
true
因为你正在寻找一个部分(如果你想要一个普通的模板,你可以省略这个参数).
我知道这已经回答了,并且已经有一百万年的历史了,但这就是我最终为我解决的问题......
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
Run Code Online (Sandbox Code Playgroud)
现在而不是打电话
<%= render "#{dynamic_path}" if lookup_context.find_all("#{dynamic_path}",[],true).any? %>
我打电话 <%= render_if_exists "#{dynamic_path}" %>
希望有所帮助.(还没试过rails3)
我曾多次使用这种范例取得了巨大的成功:
<%=
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
%>
Run Code Online (Sandbox Code Playgroud)
上述代码的好处是我们可以处理两个特定情况:
如果我们只是使用代码<%= render :partial => "#{dynamic_partial}" rescue nil %>
或某些衍生物,那么部分可能存在,但会引发一个异常,这个异常会被静默吃掉并成为调试的痛苦根源.
你自己的帮手呢:
def render_if_exists(path, *args)
render path, *args
rescue ActionView::MissingTemplate
nil
end
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
28432 次 |
最近记录: |