0
0
Ruby on Railsframework~10 mins

Page and action caching in Ruby on Rails - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to enable page caching for the 'index' action in a Rails controller.

Ruby on Rails
class ProductsController < ApplicationController
  [1] :index

  def index
    @products = Product.all
  end
end
Drag options to blanks, or click blank then click option'
Acache_page
Bcache_pages
Ccaches_page
Dpage_cache
Attempts:
3 left
💡 Hint
Common Mistakes
Using singular 'cache_page' instead of 'caches_page'.
Using non-existent methods like 'page_cache'.
2fill in blank
medium

Complete the code to enable action caching for the 'show' action in a Rails controller.

Ruby on Rails
class ArticlesController < ApplicationController
  [1] :show

  def show
    @article = Article.find(params[:id])
  end
end
Drag options to blanks, or click blank then click option'
Acaches_action
Bcache_action
Caction_cache
Dcaches_actions
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cache_action' instead of 'caches_action'.
Using plural 'caches_actions' which is incorrect.
3fill in blank
hard

Fix the error in the code to expire the cached page for the 'home' action.

Ruby on Rails
class HomeController < ApplicationController
  def expire_cache
    [1]('/home')
  end
end
Drag options to blanks, or click blank then click option'
Aremove_page
Bexpire_page
Cclear_cache
Dexpire_action
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'expire_action' which is for action caching, not page caching.
Using non-existent methods like 'clear_cache'.
4fill in blank
hard

Fill both blanks to expire the cached action for 'show' with id parameter.

Ruby on Rails
class ProductsController < ApplicationController
  def expire_show_cache
    [1](action: 'show', id: [2])
  end
end
Drag options to blanks, or click blank then click option'
Aexpire_action
Bparams[:id]
Crequest[:id]
Dexpire_page
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'expire_page' instead of 'expire_action' for action cache.
Using 'request[:id]' which is incorrect syntax.
5fill in blank
hard

Fill all three blanks to cache the 'index' action with a condition to skip caching if user is logged in.

Ruby on Rails
class PostsController < ApplicationController
  [1] :index, if: -> { ![2] }

  private

  def [3]
    current_user.present?
  end
end
Drag options to blanks, or click blank then click option'
Acaches_action
Buser_logged_in?
Clogged_in?
Duser_signed_in?
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'caches_page' instead of 'caches_action'.
Mismatch between method name and condition method.