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

When I do a partial that does not exist, I get an exception. I would like to check if a partial part exists before rendering it, and in case it does not exist, I will do 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 %> 
+85
ruby-on-rails partial
Aug 24 '10 at 17:51
source share
7 answers

I am currently using the following Rails 3 / 3.1 projects:

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

The advantage over other solutions that I have seen is that it will look in all ways of representations, and not just for your rail root. This is important to me because I have a lot of rail engines.

This also works in Rails 4.

+88
Nov 18 '11 at 9:40
source share

I also struggled with this. This is the method I ended up with:

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

Basically, if partial does not exist, do nothing. Would you like to print something if the partial part is missing?

Edit 1: Oh, I do not understand understanding. You said you want to do something else. In this 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 a partial file:

 <%= render :partial => "#{dynamic_partial}" if File.exists?(Rails.root.join("app", "views", params[:controller], "_#{dynamic_partial}.html.erb")) %> 
+64
Aug 24 '10 at 18:06
source share

From inside view, template_exists? works, but the calling convention does not work with a single partial name string, does it accept template_exists instead? (name, prefix, partial)

To check partial on the way: app / views / posts / _form.html.slim

Using:

 lookup_context.template_exists?("form", "posts", true) 
+48
Aug 26 2018-11-21T00:
source share

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

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

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

_prefixes gives the context of the controller inheritance chain.

true because you are looking for partial (you can omit this argument if you want a regular pattern).

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

+29
Jun 08 '13 at 13:29
source share

I know that this was answered, and he is a million years old, but here's how I ended it 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 this helps. (did not try in rails3)

+8
Aug 05 '15 at 18:56
source share

I have used this paradigm in many cases 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 advantage of the above code is that we can handle specific towing cases:

  • Partially missing
  • Partial exists, but for some reason it threw an error

If we just use the code <%= render :partial => "#{dynamic_partial}" rescue nil %> or some derivative, a partial may exist, but raise an exception that will be quietly used and become a source of pain for debugging.

+4
Feb 16 '16 at 5:59
source share

How about your own assistant:

 def render_if_exists(path, *args) render path, *args rescue ActionView::MissingTemplate nil end 
+1
Jul 07 '16 at 13:52
source share



All Articles