为何添加WelcomeController#index模板后仍无法解决ActionController::UnknownFormat错误?
Hey there, let's work through this frustrating 406 error you're hitting. You've done all the right initial steps—generated the Welcome controller, created the index view, set up the root route—but the error still persists. Let's break down possible causes and fixes to get your app running:
1. Double-check your view file's location and naming
First, confirm that your index.html.erb is exactly where it should be: in the app/views/welcome/ directory. A tiny typo (like index.html.er instead of .erb) or accidentally placing it in the wrong folder (e.g., app/views/ root or another controller's folder) can cause Rails to miss it entirely. macOS is case-insensitive, but it's worth verifying the folder name is welcome (not Welcome or another variation) just to be safe.
2. Make sure your controller isn't restricting response formats
Even though your generated controller looks empty, double-check that you didn't accidentally add a respond_to constraint somewhere. For example, if your controller had this code:
class WelcomeController < ApplicationController respond_to :json # This would block HTML requests def index end end
Rails would only respond to JSON requests, ignoring your HTML template entirely. Remove any respond_to lines that aren't needed, or add :html to the list if you intend to support multiple formats.
3. Clear Rails cache (it might be holding onto old data)
Rails sometimes caches view lookup paths, even in development mode. Clear out the temporary files to rule this out:
rails tmp:clear
Then restart your server with rails s and try accessing the root path again.
4. Verify the actual request format (even if the log says text/html)
While the error logs show request.formats: ["text/html"], it's worth testing with a direct curl request to eliminate browser plugin or header quirks:
curl -H "Accept: text/html" http://localhost:3000
Watch the server logs closely as you run this—if the format still shows as text/html but the error persists, we can rule out hidden header issues.
5. Address Rails 5.0.7.2-specific quirks
Your Rails version is a bit older, so there might be template engine edge cases to consider:
- If you have other template engines (like Haml or Slim) in your Gemfile, Rails might be prioritizing those over ERB. Try renaming your view to
index.html(removing the.erbextension) to force it to use the plain HTML template, or explicitly specify the format in your controller action:def index render format: :html end - Ensure your
config/application.rbdoesn't have any custom template resolver settings that could be overriding the default view lookup path.
6. Rule out route conflicts
Even though rails routes shows your root route pointing correctly, check if there's an earlier route in config/routes.rb that might be matching first. Try moving the root "welcome#index" line to the very top of the routes file, or temporarily comment out all other routes to test if the root path works in isolation.
Go through these steps one by one—chances are one of them will resolve the unknown format error for you.
内容的提问来源于stack exchange,提问作者Marchan4

