Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tips/Tricks for optimizing performance of views in Rails (2.x or 3.x)?

What can people do to optimize the rendering of views in Rails 2.x (and 3.x)?

Our application spends most of its time rendering views (minimal DB calls).

How can we accelerate performance/rendering of views?

Thanks!

like image 337
Crashalot Avatar asked May 05 '11 21:05

Crashalot


1 Answers

The recommended way to speed erb rendering is to avoid doing it. Try using page caching or conditional GET headers to avoid re-rendering content that hasn't changed.

http://guides.rubyonrails.org/caching_with_rails.html

class ProductsController < ApplicationController

  def show
    @product = Product.find(params[:id])

    # If the request is stale according to the given timestamp and etag value
    # (i.e. it needs to be processed again) then execute this block
    if stale?(:last_modified => @product.updated_at.utc, :etag => @product)
      respond_to do |wants|
        # ... normal response processing
      end
    end

    # If the request is fresh (i.e. it's not modified) then you don't need to do
    # anything. The default render checks for this using the parameters
    # used in the previous call to stale? and will automatically send a
    # :not_modified.  So that's it, you're done.
end
like image 61
Kevin Avatar answered Oct 12 '22 21:10

Kevin