Fix Pundit::NotAuthorizedError: not allowed to update? this Article in Rails
This error means the current user does not have permission to perform the requested action according to your Pundit policy. Pundit checked the policy class for the resource and the method returned false. Review your policy file to ensure the authorization logic correctly grants access to the appropriate user roles.
Reading the Stack Trace
Here's what each line means:
- pundit (2.3.2) lib/pundit/authorization.rb:110:in `authorize': Pundit's authorize method checked the ArticlePolicy#update? method and it returned false.
- app/controllers/articles_controller.rb:22:in `update': The controller calls authorize @article which triggers the policy check.
- actionpack (7.1.3) lib/abstract_controller/base.rb:224:in `process_action': The standard Rails action processing pipeline where the authorization check occurs.
Common Causes
1. Policy method returns false for the user
The policy does not grant update permission to the current user's role.
# app/policies/article_policy.rb
class ArticlePolicy < ApplicationPolicy
def update?
user.admin? # Only admins can update, but editors should too
end
end
2. Missing policy method
The policy class does not define the method for the action being authorized.
class ArticlePolicy < ApplicationPolicy
def create?
true
end
# update? is not defined, inherits from ApplicationPolicy which returns false
end
3. Wrong resource passed to authorize
The controller passes the wrong resource to authorize, causing the wrong policy to be used.
def update
@article = Article.find(params[:id])
authorize @article.category # Should be authorize @article
@article.update!(article_params)
end
The Fix
Expand the update? policy method to allow admins, editors, and the article's author to update. This follows the principle of least privilege while enabling the correct users to perform the action.
class ArticlePolicy < ApplicationPolicy
def update?
user.admin?
end
end
class ArticlePolicy < ApplicationPolicy
def update?
user.admin? || user.editor? || record.author == user
end
end
Testing the Fix
require 'rails_helper'
RSpec.describe ArticlePolicy do
let(:admin) { build(:user, role: 'admin') }
let(:editor) { build(:user, role: 'editor') }
let(:author) { build(:user) }
let(:other_user) { build(:user) }
let(:article) { build(:article, author: author) }
describe '#update?' do
it 'allows admin' do
expect(ArticlePolicy.new(admin, article).update?).to be true
end
it 'allows editor' do
expect(ArticlePolicy.new(editor, article).update?).to be true
end
it 'allows author' do
expect(ArticlePolicy.new(author, article).update?).to be true
end
it 'denies other users' do
expect(ArticlePolicy.new(other_user, article).update?).to be false
end
end
end
Run your tests:
bundle exec rspec spec/policies/article_policy_spec.rb
Pushing Through CI/CD
git checkout -b fix/rails-pundit-authorization,git add app/policies/article_policy.rb spec/policies/article_policy_spec.rb,git commit -m "fix: allow editors and authors to update articles",git push origin fix/rails-pundit-authorization
Your CI config should look something like this:
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: postgres
ports: ['5432:5432']
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: '3.3'
bundler-cache: true
- run: bin/rails db:setup
- run: bundle exec rspec
The Full Manual Process: 18 Steps
Here's every step you just went through to fix this one bug:
- Notice the error alert or see it in your monitoring tool
- Open the error dashboard and read the stack trace
- Identify the file and line number from the stack trace
- Open your IDE and navigate to the file
- Read the surrounding code to understand context
- Reproduce the error locally
- Identify the root cause
- Write the fix
- Run the test suite locally
- Fix any failing tests
- Write new tests covering the edge case
- Run the full test suite again
- Create a new git branch
- Commit and push your changes
- Open a pull request
- Wait for code review
- Merge and deploy to production
- Monitor production to confirm the error is resolved
Total time: 30-60 minutes. For one bug.
Or Let bugstack Fix It in Under 2 minutes
Every step above? bugstack does it automatically.
Step 1: Install the SDK
gem install bugstack
Step 2: Initialize
require 'bugstack'
Bugstack.init(api_key: ENV['BUGSTACK_API_KEY'])
Step 3: There is no step 3.
bugstack handles everything from here:
- Captures the stack trace and request context
- Pulls the relevant source files from your GitHub repo
- Analyzes the error and understands the code context
- Generates a minimal, verified fix
- Runs your existing test suite
- Pushes through your CI/CD pipeline
- Deploys to production (or opens a PR for review)
Time from error to fix deployed: Under 2 minutes.
Human involvement: zero.
Try bugstack Free →No credit card. 5-minute setup. Cancel anytime.
Deploying the Fix (Manual Path)
- Update the policy method with correct authorization logic.
- Add comprehensive policy specs for all user roles.
- Run the full test suite.
- Open a pull request.
- Merge and verify authorization works in staging.
Frequently Asked Questions
BugStack runs the fix through your existing test suite, generates additional edge-case tests, and validates that no other components are affected before marking it safe to deploy.
BugStack never pushes directly to production. Every fix goes through a pull request with full CI checks, so your team can review it before merging.
Add a rescue_from Pundit::NotAuthorizedError in your ApplicationController that redirects to a forbidden page or renders a 403 status.
Pundit uses plain Ruby policy objects and is more testable. CanCanCan uses a centralized Ability class. Pundit is preferred for larger applications where policies per model are cleaner.