Fix ViewComponent::TemplateError: Could not find a template file or inline render method for ButtonComponent in Rails
This error occurs when ViewComponent cannot find a template file or an inline render method for your component class. ViewComponent looks for a matching template file in the same directory as the component class or expects a call method for inline rendering. Create the template file or define a call method in the component.
Reading the Stack Trace
Here's what each line means:
- view_component (3.10.0) lib/view_component/base.rb:245:in `compile': ViewComponent tries to compile the component but cannot find a template or call method.
- view_component (3.10.0) lib/view_component/base.rb:180:in `render_in': The render_in method is invoked when the component is rendered in a view.
- app/views/shared/_form.html.erb:15:in `_app_views_shared__form_html_erb__render': The partial at line 15 renders the ButtonComponent which triggers the error.
Common Causes
1. Missing template file
The component class exists but no corresponding .html.erb template file was created.
# app/components/button_component.rb
class ButtonComponent < ViewComponent::Base
def initialize(label:, variant: :primary)
@label = label
@variant = variant
end
end
# Missing: app/components/button_component.html.erb
2. Template in wrong directory
The template exists but is in a different directory than the component class.
# Component: app/components/button_component.rb
# Template: app/views/components/button_component.html.erb (wrong location)
# Should be: app/components/button_component.html.erb
3. Missing call method for inline component
An inline component does not define the required call method.
class ButtonComponent < ViewComponent::Base
def initialize(label:)
@label = label
end
# Missing call method for inline rendering
end
The Fix
Create the template file button_component.html.erb in the same directory as the component class. ViewComponent uses a convention where the template name matches the component class name with a .html.erb extension.
# app/components/button_component.rb exists but no template
class ButtonComponent < ViewComponent::Base
def initialize(label:, variant: :primary)
@label = label
@variant = variant
end
end
# app/components/button_component.rb
class ButtonComponent < ViewComponent::Base
def initialize(label:, variant: :primary)
@label = label
@variant = variant
end
end
# app/components/button_component.html.erb
# <button class="btn btn-<%= @variant %>">
# <%= @label %>
# </button>
Testing the Fix
require 'rails_helper'
RSpec.describe ButtonComponent, type: :component do
it 'renders a button with the label' do
render_inline(ButtonComponent.new(label: 'Submit'))
expect(page).to have_button('Submit')
end
it 'applies the variant class' do
render_inline(ButtonComponent.new(label: 'Delete', variant: :danger))
expect(page).to have_css('.btn-danger')
end
it 'defaults to primary variant' do
render_inline(ButtonComponent.new(label: 'Save'))
expect(page).to have_css('.btn-primary')
end
end
Run your tests:
bundle exec rspec spec/components/button_component_spec.rb
Pushing Through CI/CD
git checkout -b fix/rails-view-component-template,git add app/components/button_component.html.erb,git commit -m "fix: add missing template for ButtonComponent",git push origin fix/rails-view-component-template
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 template file in the component directory.
- Add component specs with render_inline.
- Run the component test suite.
- Open a pull request.
- Merge and verify the component renders 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.
Use template files for complex HTML and inline call methods for simple components. The call method is cleaner for one-liner components.
Set up component previews in test/components/previews/ and visit /rails/view_components in development to see rendered previews.