1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add documentation about template partial caching

Motivation:
  - #29423 surfaced some confusion about how template caching works when
  using partials across files with different mime types. This doc
  attempts to explain how this can be accomplished.
This commit is contained in:
Cory Gwin @gwincr11 2017-07-26 19:54:41 -04:00
parent 9569a0cde8
commit 49f6c47e41

View file

@ -179,6 +179,24 @@ With `touch` set to true, any action which changes `updated_at` for a game
record will also change it for the associated product, thereby expiring the record will also change it for the associated product, thereby expiring the
cache. cache.
### Shared Partial Caching
It is possible to share partials and associated caching between files with different mime types. For example shared partial caching allows template writers to share a partial between HTML and Javascript files. When templates are collected in the template resolver file paths they only include the template language extension and not the mime type. Because of this templates can be used for multiple mime types. Both HTML and JavaScript requests will respond to the following code:
```ruby
render(partial: 'hotels/hotel', collection: @hotels, cached: true)
```
Will load a file named `hotels/hotel.erb`.
Another option is to include the full filename of the partial to render.
```ruby
render(partial: 'hotels/hotel.html.erb', collection: @hotels, cached: true)
```
Will load a file named `hotels/hotel.html.erb` in any file mime type, for example you could include this partial in a Javascript file.
### Managing dependencies ### Managing dependencies
In order to correctly invalidate the cache, you need to properly define the In order to correctly invalidate the cache, you need to properly define the