Back to Course

Practice · Reading the Source · Card 4

Why is the unauthorized user still hitting your action?

A before_action that reads like it should block non-admins. The action still runs. The bug is one missing line.

The code

A non-admin sends DELETE /posts/42. The before_action runs, but the post still gets destroyed.

class PostsController < ApplicationController
  before_action :require_admin, only: [:destroy]

  def destroy
    @post = Post.find(params[:id])
    @post.destroy
    redirect_to posts_path
  end

  private

  def require_admin
    flash[:alert] = "Admins only" unless current_user.admin?
  end
end

The question

What's the minimal change to require_admin that actually halts the callback chain?

Take a moment. Pick the best answer. Wrong picks reveal why they're wrong, which is half the point.