Complete the code to define a before filter in a Rails controller.
class PostsController < ApplicationController [1] :authenticate_user def index # list posts end end
The before_action method sets a filter to run before controller actions.
Complete the code to define an after filter that runs only on the show action.
class UsersController < ApplicationController after_action :log_view, only: [1] def show # show user end end
The only: option limits the filter to specified actions. Here, it runs only on show.
Fix the error in the filter definition to run a method after every action.
class CommentsController < ApplicationController [1] :clear_cache def create # create comment end end
In modern Rails, after_action replaces the deprecated after_filter.
Fill both blanks to run a method before all actions except edit and update.
class ProfilesController < ApplicationController [1] :check_permissions, except: [2] def edit # edit profile end def update # update profile end end
before_action runs the method before actions, and except: [:edit, :update] excludes those actions.
Fill all three blanks to define a before filter that runs only on create and update, and an after filter that runs on destroy.
class ArticlesController < ApplicationController [1] :verify_user, only: [2] [3] :log_deletion, only: [:destroy] def create # create article end def update # update article end def destroy # destroy article end end
before_action runs verify_user only on create and update. after_action runs log_deletion only on destroy.