0
0
RailsDebug / FixBeginner · 3 min read

How to Fix Missing Template Error in Rails Quickly

The missing template error in Rails happens when the controller action cannot find the matching view file. To fix it, ensure you have a correctly named view file in the right folder matching the controller and action names, or explicitly render a template with render.
🔍

Why This Happens

This error occurs because Rails expects a view template file that matches the controller action name inside the correct folder under app/views. If the file is missing, misspelled, or placed in the wrong folder, Rails cannot find it and raises the missing template error.

ruby
class PostsController < ApplicationController
  def show
    # No explicit render here
  end
end

# No file at app/views/posts/show.html.erb
Output
ActionView::MissingTemplate (Missing template posts/show, application/show with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee]}.)
🔧

The Fix

Create the missing view file with the correct name and location. For example, for PostsController#show, add app/views/posts/show.html.erb. Alternatively, explicitly render a different template or partial in the controller.

ruby+erb
# Create this file: app/views/posts/show.html.erb
<h1>Post Details</h1>
<p>Here is the post content.</p>

# Or explicitly render in controller
class PostsController < ApplicationController
  def show
    render 'custom_template'
  end
end
Output
<h1>Post Details</h1> <p>Here is the post content.</p>
🛡️

Prevention

Always name your view files to match controller actions and place them in the correct folder under app/views. Use Rails conventions to avoid errors. Use rails server logs or browser DevTools to check which template Rails tries to find. Adding explicit render calls can clarify which template is used.

Use linters or editors with Rails support to catch missing files early.

⚠️

Related Errors

  • Unknown action: Happens when the controller action is missing or misspelled.
  • Routing error: Occurs if the URL does not match any route.
  • Template handler missing: Happens if the template file has an unsupported extension.

Key Takeaways

Ensure view files are named after controller actions and placed in the correct folder.
Create missing template files or explicitly render templates in controller actions.
Check Rails logs to see which template Rails is trying to find.
Use Rails conventions to avoid missing template errors.
Use editor tools or linters to catch missing views early.