Ruby on Rails is a web application framework that follows the model-view-controller (MVC) pattern. It uses RESTful routing and conventions to map HTTP verbs like GET and POST to controller actions. Models define the data and behavior of an application, controllers handle and respond to user input, and views display the UI for users. Rails emphasizes conventions over configuration for productivity and includes features like caching, asset pipelining, and internationalization.
7. RESTful routing
HTTP Verb Path Action
GET /posts index
GET /posts/new new
POST /posts create
GET /posts/:id show
GET /posts/:id/edit edit
PUT /posts/:id update
DELETE /posts/:id destroy
16. Page caching
class ProductsController < ActionController
caches_page :index
def index
@products = Products.all
end
def create
expire_page :action => :index
end
end
17. Page caching
Appropriate for Not for
●
Public pages ●
Pages that require
●
Pages that do not authentication
change often ●
Pages that changes
often
18. Action caching
class ProductsController < ActionController
before_filter :authenticate
caches_action :index
def index
@products = Product.all
end
def create
expire_action :action => :index
end
end
19. Fragment caching
In template
<% cache('all_available_products') do %>
All available products:
<% Product.all.each do |p| %>
<%= link_to p.name, product_url(p) %>
<% end %>
<% end %>
Expire cach
expire_fragment('all_available_products')
21. Sweepers to expire caching
class ProductSweeper < ActionController::Caching::Sweeper
observe Product # This sweeper is going to keep an eye on the Product model
def after_create(product)
expire_cache_for(product)
end
def after_update(product)
expire_cache_for(product)
end
def after_destroy(product)
expire_cache_for(product)
end
private
def expire_cache_for(product)
expire_page(:controller => 'products', :action => 'index')
expire_fragment('all_available_products')
end
end
22. Sweepers to expire caching
class ProductsController < ActionController
before_filter :authenticate
caches_action :index
cache_sweeper :product_sweeper
def index
@products = Product.all
end
end
23. Configure caching
ActionController::Base.cache_store = :memory_store
or
ActionController::Base.cache_store = :file_store, "/path/to/cache/directory"
or
ActionController::Base.cache_store = :mem_cache_store, "cache-1.example.com"
24. Test performance
●
Benchmarking web site performance
●
class HomepageTest < ActionDispatch::PerformanceTest
# Replace this with your real tests.
def test_homepage
get '/'
end
end
http://guides.rubyonrails.org/performance_testing.html