sjsc
sjsc

Reputation: 4632

Ruby on Rails: Why does calling a partial in my views take an extended time to render?

For example, I'm calling three partials in my recipes/_cuisines.html.erb file:

  <%= render "recipes/cuisines/denmark.html %>
  <%= render "recipes/cuisines/spain.html %>
  <%= render "recipes/cuisines/italy.html %>

Nothing else is in that file. The partials are just static content with no collections. This is what I get from my logs:

  Rendered recipes/cuisines/_denmark.html.erb (4.6ms) [cache miss]
  Rendered recipes/cuisines/_spain.html.erb (2.3ms) [cache miss]
  Rendered recipes/cuisines/_italy.html.erb (5.3ms) [cache miss]
  Rendered recipes/_cuisines.html.erb (161.5ms) [cache miss]

If I remove one of the partials, I get something like:

  Rendered recipes/cuisines/_denmark.html.erb (4.6ms) [cache miss]
  Rendered recipes/cuisines/_spain.html.erb (2.3ms) [cache miss]
  Rendered recipes/_cuisines.html.erb (101.7ms) [cache miss]

Removing another partial, I get:

  Rendered recipes/cuisines/_denmark.html.erb (4.6ms) [cache miss]
  Rendered recipes/_cuisines.html.erb (50.5ms) [cache miss]

Removing every partial, I get something like this:

  Rendered recipes/_cuisines.html.erb (0.9ms) [cache miss]

It seems that it takes an extraordinary time to render each partial even if the partial itself is very small. Do you know what causes that and what I can do to call partials faster? Is it recommended to not call partials at all?

(I'm using Rails 5.1.1 and ruby 2.5.0dev)

Upvotes: 4

Views: 409

Answers (1)

Pramod
Pramod

Reputation: 1416

I believe you have pasted logs from development environment. If so, then it is due to config.cache_classes which is default to false for development and to true for test and production environments as mentioned in the rails guides.

config.cache_classes controls whether or not application classes and modules should be reloaded on each request. Defaults to false in development mode, and true in test and production modes.

config.action_view.cache_template_loading controls whether or not templates should be reloaded on each request. Defaults to whatever is set for config.cache_classes.

I suggest to run the application in test or production environments and then check the logs.

Upvotes: 2

Related Questions