0
0
Ruby on Railsframework~30 mins

Page and action caching in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
Page and Action Caching in Rails
📖 Scenario: You are building a simple blog application in Rails. To improve performance, you want to cache the homepage and the show page of posts so that users get faster responses.
🎯 Goal: Learn how to implement page caching for the homepage and action caching for the post show page in a Rails app.
📋 What You'll Learn
Create a controller with an index action for the homepage
Create a controller with a show action for posts
Set up page caching for the homepage
Set up action caching for the post show page
💡 Why This Matters
🌍 Real World
Caching pages and actions helps websites load faster by saving rendered content and reusing it for multiple users.
💼 Career
Understanding Rails caching is important for backend developers to optimize web app performance and reduce server load.
Progress0 / 4 steps
1
Create PostsController with index and show actions
Create a Rails controller called PostsController with two actions: index and show. The index action should assign @posts to Post.all. The show action should assign @post to Post.find(params[:id]).
Ruby on Rails
Need a hint?

Use class PostsController < ApplicationController and define index and show methods.

2
Enable page caching for the index action
Add page caching for the index action in PostsController by calling caches_page :index inside the controller class.
Ruby on Rails
Need a hint?

Use caches_page :index inside the controller class but outside any method.

3
Enable action caching for the show action
Add action caching for the show action in PostsController by calling caches_action :show inside the controller class.
Ruby on Rails
Need a hint?

Use caches_action :show inside the controller class but outside any method.

4
Add routes for posts index and show
In the config/routes.rb file, add resourceful routes for posts by writing resources :posts, only: [:index, :show].
Ruby on Rails
Need a hint?

Add resources :posts, only: [:index, :show] inside the Rails.application.routes.draw do ... end block.