igmarin/rails-agent-skills

implement-authorization

Use when adding or reviewing Rails authorization with Pundit, CanCanCan, or policy objects. Trigger words: authorization, Pundit, CanCanCan, policy, roles, permissions.

First seen Aug 14, 2026

Installation

$ npx skills add igmarin/rails-agent-skills --skill implement-authorization

Similar popular skills

Related neighbors and high-traction skills in the same topics — useful to compare before installing.

Also in this package

Other skills from igmarin/rails-agent-skills · top by installs.

npx skills add igmarin/rails-agent-skills

Browse all from igmarin/rails-agent-skills

More details

Agent compatibility

Declared targets from SKILL.md / docs. Unmarked agents are not listed — the skill may still install via the CLI.

Claude Code Not declared
Cursor Not declared
Codex Not declared
GitHub Copilot Not declared
Windsurf Not declared
Gemini CLI Not declared
Cline Not declared
OpenCode Not declared

Repository health

Stars 24
License LICENSE
Default branch main
Open issues 0
Status Active

Skill metadata

Parsed from SKILL.md frontmatter.

Version1.0.0
LicenseMIT
More metadata
version
1.0.0
user-invocable
true

Package contents

Files included with this skill beyond the listing page.

  • skill md SKILL.md 4,613 B
  • docs SUMMARY.md 199 B

History

  1. First seen on skills.sh
  2. First recorded snapshot · 17 installs

SKILL.md

Implement Authorization

Quick Reference

Gem Pattern Best For
Pundit Explicit policy classes Complex per-resource rules
CanCanCan Centralized Ability class Simple role-based permissions

Core Process

Implementation Workflow

  1. Add gem — add pundit or cancancan to Gemfile and run bundle install
  2. Generate base — run the gem's installer (rails g pundit:install or rails g cancan:ability)
  3. Define policies/abilities — create policy classes (Pundit) or populate the Ability class (CanCanCan); always use policy objects, never inline authorization logic in controllers
  4. Authorize in controllers — call authorize @record (Pundit) or authorize! :action, @record (CanCanCan) in each action
  5. Verify authorization — attempt an unauthorized action in the browser or console and confirm it raises Pundit::NotAuthorizedError or CanCan::AccessDenied as expected; use persisted records (e.g., User.create!) not unsaved ones
  6. Scope queries — use policyscope(Model) or accessibleby(current_ability) for index actions
  7. Test all roles — write policy specs and request specs covering admin, owner, and guest; check specific permissions, never presence checks alone

Patterns

Pundit

class PostPolicy < ApplicationPolicy
  def update?
    user.admin? || record.user_id == user.id
  end
end

CanCanCan

class Ability
  include CanCan::Ability

  def initialize(user)
    can :update, Post, user_id: user.id
    can :manage, :all if user.admin?
  end
end

Troubleshooting

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 afteraction :verifyauthorized 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

Testing

Cover every role (admin, owner, guest) in both policy specs and request specs.

Minimal Pundit policy spec

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

Output Style

When implementing or reviewing authorization, the output answer.md must include:

  1. Manual Denied-Action Verification — a dedicated section with simulated Rails console output showing the authorization exception raised when an unauthorized action is attempted. Always use persisted records (User.create!, Post.create!), never unsaved ones.
  2. HTTP and Policy Verification — concrete curl requests or controller test commands with expected HTTP response codes (e.g. 403 Forbidden or 302 Found) when access is denied.
  3. Language — English unless explicitly requested otherwise.

See [references/output-style.md](references/output-style.md) for full formatting examples including Pundit and CanCanCan console output templates.

Integration

Skill When to chain
write-tests When implementing authorization tests.

Extended Resources (Progressive Disclosure)

Load these files only when their specific content is needed:

  • [EXAMPLES.md](EXAMPLES.md) — Use when you need complete Pundit or CanCanCan implementation examples beyond the inline samples
  • [references/workflow.md](references/workflow.md) — Use when you need the step-by-step authorization implementation workflow diagram
  • [references/output-style.md](references/output-style.md) — Use when you need full formatting templates for console verification output