/ruby-on-rails:implement-authorizationfrom ruby-on-rails
Use when adding or reviewing Rails authorization with Pundit, CanCanCan, or policy objects. Trigger words: authorization, Pundit, CanCanCan, policy, roles, permissions.
Ships in ruby-on-rails. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
| Gem | Pattern | Best For |
|---|---|---|
| Pundit | Explicit policy classes | Complex per-resource rules |
| CanCanCan | Centralized Ability class | Simple role-based permissions |
pundit or cancancan only if the task requires a new framework and project conventions permit itrails g pundit:install or rails g cancan:ability) only when no supported framework is present and the project permits the generator; otherwise proceed with the detected framework without invoking generatorsauthorize @record (Pundit) or authorize! :action, @record (CanCanCan) in each actionPundit::NotAuthorizedError or CanCan::AccessDenied as expected; use persisted records (e.g., User.create!) not unsaved onespolicy_scope(Model) or accessible_by(current_ability) for index actionsclass PostPolicy < ApplicationPolicy
def update?
user.admin? || record.user_id == user.id
end
end
class Ability
include CanCan::Ability
def initialize(user)
can :update, Post, user_id: user.id
can :manage, :all if user.admin?
end
end
| Error | Likely Cause | Fix |
|---|---|---|
Pundit::NotDefinedError | No policy class found for the record | Create app/policies/model_policy.rb inheriting from ApplicationPolicy |
Pundit::AuthorizationNotPerformedError | authorize not called in a controller action | Add authorize @record in the action, or after_action :verify_authorized to catch misses |
CanCan::AccessDenied unexpectedly raised | Ability rules not matching the current user/role | Inspect current_ability.can?(:action, @record) in the console to debug rule evaluation |
Cover every role (admin, owner, guest) in both policy specs and request specs.
RSpec.describe PostPolicy do
subject { described_class.new(user, post) }
let(:post) { create(:post, user: owner) }
let(:owner) { create(:user) }
context 'as admin' do
let(:user) { create(:user, :admin) }
it { is_expected.to permit_action(:update) }
end
context 'as owner' do
let(:user) { owner }
it { is_expected.to permit_action(:update) }
end
context 'as guest' do
let(:user) { create(:user) }
it { is_expected.not_to permit_action(:update) }
end
end
When implementing or reviewing authorization, the output answer.md must include:
User.create!, Post.create!), never unsaved ones.curl requests or controller test commands with expected HTTP response codes (e.g. 403 Forbidden or 302 Found) when access is denied.See references/output-style.md for full formatting examples including Pundit and CanCanCan console output templates.
| Skill | When to chain |
|---|---|
| write-tests | When implementing authorization tests. |
Load these files only when their specific content is needed:
What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub fcakiroglu16/marketplace --plugin ruby-on-rails7 skills in this plugin
/implement-background-jobUse when adding or reviewing an Active Job / Sidekiq / Solid Queue worker. Cover idempotency, retry_on, and discard_on. Trigger words: background job, Active Job, Sidekiq, Solid Queue, worker./implement-hotwireUse when adding Turbo Frames, Turbo Streams, or Stimulus controllers. Trigger words: Hotwire, Turbo, Stimulus, frames, streams./rails-patternsUse when writing or modifying code in a Rails application. Guides agents to follow thoughtbot best practices for N+1 prevention, view layer organization, and domain modeling.Other plugins' skills for the same job