---
title: "Rails 5 Wildcard for specifying template dependencies"
description:
  "We can now use wildcard for adding all template dependencies from a directory
  rather than adding files one by one for cache digests"
canonical_url: "https://www.bigbinary.com/blog/rails-5-allows-wildcard-for-specifying-template-dependencies"
markdown_url: "https://www.bigbinary.com/blog/rails-5-allows-wildcard-for-specifying-template-dependencies.md"
---

# Rails 5 Wildcard for specifying template dependencies

We can now use wildcard for adding all template dependencies from a directory
rather than adding files one by one for cache digests

- Author: Abhishek Jain
- Published: August 17, 2016
- Categories: Rails 5, Rails

### Cache Digests

After cache digests were introduced in Rails, all calls to `#cache` in views
automatically append a digest of that template and all of its dependencies to
the cache key.

So developers no longer need to manually discard cache for the specific
templates they make changes to.

```ruby

# app/views/users/show.html.erb
<% cache user do %>
  <h1>All Posts</h1>
  <%= render user.posts %>
<% end %>

# app/views/posts/_post.html.erb
<% cache post do %>
  <p> <%= post.content %></p>
  <p> <%= post.created_at.to_s %>
  <%= render 'posts/completed' %>
<% end %>

```

This creates a caching key something like this
`views/users/605416233-20129410191209/d9fb66b12bx8edf46707c67ab41d93cb2` which
depends upon the template and its dependencies.

So, now if we change `posts/_completed.html.erb`, it will change cache key and
thus it allows cache to expire automatically.

### Explicit dependencies

As we saw in our earlier example, Rails was able to determine template
dependencies implicitly. But, sometimes it is not possible to determine
dependencies at all.

Let's see an example below.

```ruby

# app/views/users/show.html.erb
<% cache user do %>
  <h1>All Posts</h1>
  <%= render user.posts %>
<% end %>

# app/views/posts/_post.html.erb
<% cache post do %>
  <p> <%= post.content %></p>
  <p> <%= post.created_at.to_s %>
  <%= render_post_complete_or_not(post) %>
<% end %>

# app/helpers/posts_helper.rb

module PostsHelper
  def render_post_complete_or_not(post)
    if post.completed?
      render 'posts/complete'
    else
      render 'posts/incomplete'
    end
  end
end

```

To explicitly add dependency on this template, we need to add a
[comment in special format](http://api.rubyonrails.org/classes/ActionView/Helpers/CacheHelper.html#method-i-cache-label-Explicit+dependencies)
as follows.

```ruby
  <%# Template Dependency: posts/complete %>
  <%# Template Dependency: posts/incomplete %>
```

If we have multiple dependencies, we need to add special comments for all the
dependencies one by one.

```ruby

# app/views/posts/_post.html.erb
<% cache post do %>
  <p> <%= post.content %></p>
  <p> <%= post.created_at.to_s %>

  <%# Template Dependency: posts/complete %>
  <%# Template Dependency: posts/incomplete %>
  <%= render_post_complete_or_not(post) %>
<% end %>

```

### Using Wildcard in Rails 5

In Rails 5, we can now use a
[wildcard for adding dependencies](https://github.com/rails/rails/pull/20904) on
multiple files in a directory. So, instead of adding files one by one we can add
dependency using wildcard.

```ruby

# app/views/posts/_post.html.erb
<% cache post do %>
  <p> <%= post.content %></p>
  <p> <%= post.created_at.to_s %>

  <%# Template Dependency: posts/* %>
  <%= render_post_complete_or_not(post) %>
<% end %>

```

## Links

- [Human page](https://www.bigbinary.com/blog/rails-5-allows-wildcard-for-specifying-template-dependencies)
