Fix Error: Error connecting Stimulus controller: 'search' controller is not registered in Rails
This error occurs when Stimulus cannot find a registered controller matching the data-controller attribute in your HTML. The controller file may be missing, not imported in the controllers index, or named incorrectly. Ensure the file naming convention matches and the controller is properly registered with the Stimulus application instance.
Reading the Stack Trace
Here's what each line means:
- at Application.connectController (stimulus.min.js:1:8234): Stimulus tries to connect a controller to the DOM element but cannot find the registered controller class.
- at Router.scopeConnected (stimulus.min.js:1:4321): The router detected a data-controller attribute and tried to connect the matching controller.
- at app/javascript/controllers/application.js:8:1: The Stimulus application initialization where controllers should be registered.
Common Causes
1. Controller file not created
The HTML references a controller that does not have a corresponding JavaScript file.
<!-- HTML references 'search' controller -->
<div data-controller="search">
<input data-search-target="input" type="text">
</div>
<!-- But app/javascript/controllers/search_controller.js does not exist -->
2. Controller not registered in index
The controller file exists but is not imported and registered in the controllers index file.
// app/javascript/controllers/index.js
import { application } from './application'
// search_controller is not imported here
3. Naming convention mismatch
The file name does not follow Stimulus naming conventions.
// File: app/javascript/controllers/searchController.js (wrong)
// Should be: app/javascript/controllers/search_controller.js
import { Controller } from '@hotwired/stimulus'
export default class extends Controller {
connect() { console.log('connected') }
}
The Fix
Create the controller file following Stimulus naming conventions: the file must be named search_controller.js for a data-controller='search' attribute. If using importmap, ensure pin_all_from covers the controllers directory.
<!-- Controller referenced but file missing or not registered -->
<div data-controller="search">
<input data-search-target="input" type="text">
</div>
// app/javascript/controllers/search_controller.js
import { Controller } from '@hotwired/stimulus'
export default class extends Controller {
static targets = ['input', 'results']
connect() {
console.log('Search controller connected')
}
search() {
const query = this.inputTarget.value
// Perform search logic
}
}
Testing the Fix
require 'rails_helper'
RSpec.describe 'Search Stimulus controller', type: :system do
it 'connects the search controller' do
visit search_path
expect(page).to have_css('[data-controller="search"]')
end
it 'has the search input target' do
visit search_path
expect(page).to have_css('[data-search-target="input"]')
end
end
RSpec.describe 'JavaScript modules', type: :request do
it 'loads without errors' do
get root_path
expect(response.body).not_to include('controller is not registered')
end
end
Run your tests:
bundle exec rspec spec/system/search_spec.rb
Pushing Through CI/CD
git checkout -b fix/rails-stimulus-controller,git add app/javascript/controllers/search_controller.js,git commit -m "fix: create search stimulus controller with proper naming",git push origin fix/rails-stimulus-controller
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)
- Create the missing Stimulus controller file with correct naming.
- Ensure pin_all_from or explicit imports register the controller.
- Run system specs to verify the controller connects.
- Open a pull request.
- Merge and verify Stimulus interactions work 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 rails generate stimulus controller_name to create the file with the correct naming convention and register it automatically.
Yes, Stimulus works independently from Turbo. You can use Stimulus alone for adding JavaScript behavior to server-rendered HTML without any real-time features.