0
0
Ruby on Railsframework~20 mins

Before and after filters in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rails Filters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Rails controller with before_action?

Consider this Rails controller snippet. What will be printed when the show action is called?

Ruby on Rails
class ArticlesController < ApplicationController
  before_action :set_article, only: [:show]

  def show
    render plain: @article.title
  end

  private

  def set_article
    @article = OpenStruct.new(title: "Hello World")
  end
end
Anil
BRaises NoMethodError
C"" (empty string)
D"Hello World"
Attempts:
2 left
💡 Hint

Remember that before_action runs the method before the action method.

lifecycle
intermediate
2:00remaining
Which callback runs after the action method in Rails controllers?

Given these Rails controller callbacks, which one runs after the action method?

Ruby on Rails
class UsersController < ApplicationController
  before_action :authenticate_user
  after_action :log_action

  def index
    render plain: "User list"
  end

  private

  def authenticate_user
    # authentication logic
  end

  def log_action
    # logging logic
  end
end
Abefore_action :authenticate_user
Baround_action :wrap_action
Cafter_action :log_action
Dskip_before_action :authenticate_user
Attempts:
2 left
💡 Hint

Think about which callback runs after the controller action finishes.

📝 Syntax
advanced
2:00remaining
What error does this Rails filter code raise?

Examine this Rails controller code. What error will it raise when loaded?

Ruby on Rails
class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit]

  def show
    render plain: @product.name
  end

  private

  def set_product
    @product = Product.find(params[:id])
  end
end
ANo error, code runs fine
BNoMethodError: undefined method `find' for nil:NilClass
CArgumentError: wrong number of arguments (given 0, expected 1)
DNameError: undefined local variable or method `params' for ProductsController
Attempts:
2 left
💡 Hint

Consider how params is available in controller methods.

🔧 Debug
advanced
2:00remaining
Does this after_action run when the action redirects?

In this Rails controller, will the after_action callback run?

Ruby on Rails
class OrdersController < ApplicationController
  after_action :clear_cart, only: [:create]

  def create
    # order creation logic
    redirect_to order_path(1)
  end

  private

  def clear_cart
    session[:cart] = nil
  end
end
AYes, the after_action runs even if the action redirects
BNo, the method clear_cart is private and cannot be called as a callback
CNo, the after_action is set for :create but the action name is misspelled
DNo, after_action callbacks do not run if the action redirects
Attempts:
2 left
💡 Hint

Redirects do not prevent after_action callbacks from running.

🧠 Conceptual
expert
3:00remaining
What is the order of execution for these Rails controller callbacks?

Given these callbacks in a Rails controller, what is the correct order they run when an action is called?

Ruby on Rails
class CommentsController < ApplicationController
  before_action :authenticate_user
  around_action :wrap_in_transaction
  after_action :log_comment

  def create
    # create comment logic
    render plain: "Created"
  end

  private

  def authenticate_user
    # authentication
  end

  def wrap_in_transaction
    ActiveRecord::Base.transaction do
      yield
    end
  end

  def log_comment
    # logging
  end
end
A2,1,3,5,4
B1,2,3,4,5
C1,3,2,4,5
D2,3,1,4,5
Attempts:
2 left
💡 Hint

Remember that around_action wraps the action and other callbacks.