Bird
Raised Fist0
Ruby on Railsframework~20 mins

Page and action caching in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rails Caching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a cached page is expired in Rails page caching?

Consider a Rails app using page caching. When the cached page is expired, what is the next behavior when a user requests that page?

ARails serves the stale cached page until the cache is manually refreshed.
BRails redirects the user to the homepage automatically.
CRails returns a 404 error because the cached page is missing.
DRails regenerates the page by running the controller action and updates the cache before serving.
Attempts:
2 left
💡 Hint

Think about how caching helps serve fresh content after expiration.

📝 Syntax
intermediate
2:00remaining
Which code snippet correctly enables action caching in a Rails controller?

Identify the correct way to enable action caching for the show action in a Rails controller.

Acaches_page :show
Bcache_action :show
Ccaches_action :show
Dcache_page :show
Attempts:
2 left
💡 Hint

Look for the exact Rails method name for action caching.

🔧 Debug
advanced
2:00remaining
Why does the cached page not update after data changes in Rails page caching?

A Rails app uses page caching, but after updating the database, the cached page still shows old data. What is the most likely cause?

AThe cache was not expired or deleted after the data update.
BThe controller action is missing the <code>caches_action</code> call.
CThe view template has syntax errors preventing cache refresh.
DThe server is running in development mode, which disables caching.
Attempts:
2 left
💡 Hint

Think about what triggers cache refresh in page caching.

🧠 Conceptual
advanced
2:00remaining
What is a key difference between page caching and action caching in Rails?

Choose the statement that best describes a fundamental difference between page caching and action caching.

APage caching stores the entire response as a static file, bypassing Rails, while action caching runs filters before serving cached content.
BAction caching stores static files on disk, while page caching caches only controller variables.
CPage caching requires database queries on every request, action caching does not.
DAction caching is faster because it skips the controller entirely.
Attempts:
2 left
💡 Hint

Consider how each caching type interacts with the Rails stack.

❓ state_output
expert
3:00remaining
What is the output after running this Rails action caching code snippet?

Given the following Rails controller snippet with action caching enabled, what will be the value of @count after two consecutive requests?

Ruby on Rails
class ItemsController < ApplicationController
  caches_action :index

  def index
    @count ||= 0
    @count += 1
    render plain: "Count: #{@count}"
  end
end
A"Count: 1" on first request, "Count: 2" on second request
B"Count: 1" on first request, "Count: 1" on second request
C"Count: 0" on first request, "Count: 1" on second request
D"Count: 2" on first request, "Count: 3" on second request
Attempts:
2 left
💡 Hint

Remember how action caching serves cached content without rerunning the action.

Practice

(1/5)
1. What is the main difference between page caching and action caching in Rails?
easy
A. Page caching requires database queries, action caching does not.
B. Page caching saves only JSON data, action caching saves HTML pages.
C. Action caching is faster because it skips filters, page caching runs filters every time.
D. Page caching saves the full HTML page, while action caching saves the controller action output with filters.

Solution

  1. Step 1: Understand what page caching does

    Page caching saves the entire HTML page as a static file, so the web server can serve it directly without hitting Rails.
  2. Step 2: Understand what action caching does

    Action caching saves the output of a controller action but still runs filters like authentication before serving cached content.
  3. Final Answer:

    Page caching saves the full HTML page, while action caching saves the controller action output with filters. -> Option D
  4. Quick Check:

    Page caching = full HTML, action caching = action output + filters [OK]
Hint: Page caching skips filters; action caching runs filters first [OK]
Common Mistakes:
  • Confusing page caching with action caching filters
  • Thinking action caching saves static files
  • Assuming page caching runs controller filters
2. Which of the following is the correct syntax to enable page caching for the index action in a Rails controller?
easy
A. caches_page :index
B. cache_page :index
C. page_cache :index
D. enable_page_cache :index

Solution

  1. Step 1: Recall Rails page caching syntax

    Rails uses the method caches_page followed by the action name symbol to enable page caching.
  2. Step 2: Verify the correct method name

    The correct method is caches_page :index. Other options are invalid method names.
  3. Final Answer:

    caches_page :index -> Option A
  4. Quick Check:

    Page caching method = caches_page [OK]
Hint: Page caching uses 'caches_page' method with action name [OK]
Common Mistakes:
  • Using 'cache_page' instead of 'caches_page'
  • Confusing page caching syntax with action caching
  • Adding parentheses incorrectly
3. Given this Rails controller snippet with action caching:
class ProductsController < ApplicationController
  caches_action :show

  before_action :authenticate_user!

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

What happens when a cached show page is requested by a user who is not authenticated?
medium
A. The user is redirected to login because filters run before serving cached content.
B. The cached page is served directly without authentication check.
C. An error occurs because cache skips filters.
D. The cache is ignored and the action runs fresh.

Solution

  1. Step 1: Understand action caching behavior with filters

    Action caching runs controller filters like before_action before serving cached content.
  2. Step 2: Apply to authentication filter

    The authenticate_user! filter runs before serving the cached show page, so unauthenticated users are redirected.
  3. Final Answer:

    The user is redirected to login because filters run before serving cached content. -> Option A
  4. Quick Check:

    Action caching runs filters before cache [OK]
Hint: Action caching runs filters before serving cache [OK]
Common Mistakes:
  • Thinking cached page skips authentication
  • Assuming cache serves static files directly
  • Believing cache is ignored if filters exist
4. You added caches_page :index to your controller but the page does not seem cached. Which of the following is the most likely cause?
medium
A. You forgot to run rake tmp:cache:clear to clear old cache.
B. You need to add skip_before_action for caching to work.
C. Your web server is not configured to serve cached files from public/ directory.
D. Page caching only works with JSON responses.

Solution

  1. Step 1: Understand how page caching serves cached pages

    Page caching saves static HTML files in the public/ folder, served directly by the web server.
  2. Step 2: Check web server configuration

    If the web server (like Nginx or Apache) is not set to serve files from public/, cached pages won't be served.
  3. Final Answer:

    Your web server is not configured to serve cached files from public/ directory. -> Option C
  4. Quick Check:

    Web server must serve public/ for page caching [OK]
Hint: Page cache files served by web server from public/ folder [OK]
Common Mistakes:
  • Assuming Rails serves cached pages directly
  • Clearing cache without checking server config
  • Thinking skip_before_action is needed for caching
5. You want to cache the show action of a controller but only for logged-in users, and you want filters like authentication to run before serving cached content. Which caching method should you use and how?
hard
A. Use cache_fragment :show inside the view to cache only parts of the page.
B. Use caches_action :show to cache with filters running before serving.
C. Use caches_page :show because it caches faster and skips filters.
D. Disable caching because caching with authentication is not possible.

Solution

  1. Step 1: Identify caching method that runs filters

    Action caching runs filters like authentication before serving cached content, suitable for logged-in user pages.
  2. Step 2: Apply correct syntax for action caching

    Use caches_action :show in the controller to cache the action output with filters.
  3. Final Answer:

    Use caches_action :show to cache with filters running before serving. -> Option B
  4. Quick Check:

    Action caching = cache + filters [OK]
Hint: Action caching runs filters; page caching skips them [OK]
Common Mistakes:
  • Choosing page caching which skips filters
  • Confusing fragment caching with action caching
  • Thinking caching can't work with authentication
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard