Question

Say the view is in erb/haml, but is static (doesn't have any dynamic parts within it), does Rails recompile that view every time someone hits it, or does it cache the html after someone hits it once?

A follow up question: if I have a view with some dynamic parts, does Rails recompile only the dynamic part of the view or does it recompile the whole page?

I'm running Rails 4.

Was it helpful?

Solution

Rails evaluates view files and partials on every request. That is why html fragment caching is so valuable.

See Caching with Rails in Rails Guides.

Typically you would use Rails' cache to cache a html fragment so it does not need to be re-rendered on each request. Here's a Haml example:

- cache "key-name-for-static-content" do
  .some-html
    some content

See DHH's How Key-Based Cache Expiration Works for key-based caching using models.

For advanced uses, I wrote cache_rocket to help cache static content around dynamic content in partials.

OTHER TIPS

Just a bit of terminology note: now compile is commonly referring to the act of turning the view template into a ruby function. Rails does this for all views:

http://guides.rubyonrails.org/action_view_overview.html#template-caching

Those functions will then output the output you want.

So I would say views are not "re-compiled" but "re-evaluated".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top