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

Ruby on-RailsPartial

Ruby on-Rails Problem Overview


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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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.

Solution 2 - Ruby on-Rails

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

Solution 3 - Ruby on-Rails

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)

Solution 4 - Ruby on-Rails

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

Solution 5 - Ruby on-Rails

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)

Solution 6 - Ruby on-Rails

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.

Solution 7 - Ruby on-Rails

What about your own helper:

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

Solution 8 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel CukierView Question on Stackoverflow
Solution 1 - Ruby on-RailsReinView Answer on Stackoverflow
Solution 2 - Ruby on-RailsJeffView Answer on Stackoverflow
Solution 3 - Ruby on-RailsLuke ImhoffView Answer on Stackoverflow
Solution 4 - Ruby on-RailsFlackouView Answer on Stackoverflow
Solution 5 - Ruby on-RailsafxjzsView Answer on Stackoverflow
Solution 6 - Ruby on-Railsbr3ntView Answer on Stackoverflow
Solution 7 - Ruby on-RailsAndreyView Answer on Stackoverflow
Solution 8 - Ruby on-RailsSergio GonzalezView Answer on Stackoverflow