Fix ActionController::RoutingError: No route matches [GET] "/api/v1/users" in Rails
This error means Rails cannot find a route matching the requested URL and HTTP method. Check your config/routes.rb file to ensure the route is defined with the correct path, HTTP verb, and namespace. Run bin/rails routes to see all available routes and compare them against the request URL.
Reading the Stack Trace
Here's what each line means:
- actionpack (7.1.3) lib/action_dispatch/middleware/debug_exceptions.rb:65:in `call': The debug exceptions middleware catches the routing error and displays a diagnostic page in development.
- actionpack (7.1.3) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call': This middleware intercepts the error to render an appropriate error response.
- railties (7.1.3) lib/rails/rack/logger.rb:40:in `call_app': The request was logged before the routing failure occurred.
Common Causes
1. Missing namespace in routes
The route is defined without the proper API namespace, so the namespaced URL does not match.
# config/routes.rb
Rails.application.routes.draw do
resources :users # maps to /users, not /api/v1/users
end
2. Incorrect HTTP verb
The route exists but is defined for a different HTTP method than the one being used in the request.
# config/routes.rb
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
post 'users', to: 'users#create' # POST only, no GET
end
end
end
3. Route defined after catch-all
A catch-all route is defined before the specific route, so the request never reaches the intended route.
# config/routes.rb
Rails.application.routes.draw do
get '*path', to: 'pages#not_found'
namespace :api do
namespace :v1 do
resources :users
end
end
end
The Fix
Wrap the users resource inside the api/v1 namespace blocks so the generated routes match the expected URL pattern /api/v1/users. Use bin/rails routes to verify.
# config/routes.rb
Rails.application.routes.draw do
resources :users
end
# config/routes.rb
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :users, only: [:index, :show, :create, :update, :destroy]
end
end
end
Testing the Fix
require 'rails_helper'
RSpec.describe 'Users API', type: :request do
describe 'GET /api/v1/users' do
it 'returns a successful response' do
get '/api/v1/users'
expect(response).to have_http_status(:ok)
end
it 'returns JSON content type' do
get '/api/v1/users'
expect(response.content_type).to include('application/json')
end
end
end
Run your tests:
bundle exec rspec spec/requests/users_spec.rb
Pushing Through CI/CD
git checkout -b fix/rails-routing-error,git add config/routes.rb spec/requests/users_spec.rb,git commit -m "fix: add API v1 namespace to users routes",git push origin fix/rails-routing-error
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)
- Verify routes locally with bin/rails routes | grep users.
- Run request specs to confirm the route resolves.
- Open a pull request with the routes change.
- Wait for CI to pass and get approval.
- Merge to main and verify routes 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.
Run bin/rails routes in your terminal to see every defined route with its HTTP method, URL pattern, and controller action. You can also visit /rails/info/routes in development.
Use namespace when you want the URL prefix and module nesting for controllers. Use scope if you only want the URL prefix without changing the controller directory structure.