Ruby on Rails中ActionController::UnknownFormat错误求助
Let's break down what's causing your error and fix it step by step:
First, Understand the Root Cause
The ActionController::UnknownFormat error pops up because there's a mismatch between the request format sent when navigating from index.html.erb to your new page, and how your controller is set up to respond. Plus, your current view-switching logic is overcomplicating things with unnecessary AJAX calls.
Step 1: Fix the Index-to-New Navigation Link
First, check the link_to in your index.html.erb that points to the new page. Do NOT add :remote => true here—that sends an AJAX (JS format) request, but you just want to load a standard HTML page:
# index.html.erb - correct the navigation link <%= link_to "Create New Class", new_klass_path %>
If this link had :remote => true, it would ask Rails for a JS response, but you don't have a matching new.js.erb template, which triggers the format error.
Step 2: Simplify View Switching (No AJAX Needed)
Your current setup uses AJAX for a simple show/hide toggle, which is unnecessary. We can handle this entirely with front-end JS, no server calls required:
Update new.html.erb
Remove the :remote => true from the toggle link (we'll use a dummy # href and prevent default behavior):
<p>Or select from previously created classes <%= link_to "here", "#", id: "link_to" %></p> <div id="previous"> <%= render partial: 'select_previous' %> </div> <div id="content"> <!-- Your Ruby form goes here --> </div>
Fix the Toggle JavaScript
Move your logic to a dedicated JS file (like app/assets/javascripts/klasses.js) or inline it in new.html.erb, and simplify the toggle:
$(document).ready(function() { // Set initial state: hide the "previous classes" section by default $("#previous").hide(); $("#link_to").click(function(e) { // Stop the link from reloading the page e.preventDefault(); // Toggle visibility of both sections $("#content").toggle(); $("#previous").toggle(); // Optional: Switch the link text for clarity $(this).text($(this).text() === "here" ? "Back to create new" : "here"); }); });
Step 3: Clean Up the Controller
Since we're removing the unnecessary AJAX call for the toggle, you can simplify your new action in the Klasses controller—no need for the respond_to block unless you plan to add AJAX functionality later:
def new @klass = Klass.new # Remove the respond_to block entirely; Rails defaults to handling HTML requests end
Why This Works
Your original error happened because:
- If your index link used
:remote => true, it sent a JS-format request to thenewaction. - Rails couldn't find a
new.js.erbtemplate to respond with, so it threw theUnknownFormaterror. - Even if you had the template, AJAX-loaded content doesn't trigger
document.ready, so your toggle logic wouldn't have worked reliably anyway.
By handling the toggle entirely on the front end, we eliminate the AJAX request that was causing the format mismatch, and make the UI snappier too.
内容的提问来源于stack exchange,提问作者Cristian Polanco

