You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ruby on Rails中ActionController::UnknownFormat错误求助

Fixing ActionController::UnknownFormat Error When Navigating to New Page in Rails

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.


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:

  1. If your index link used :remote => true, it sent a JS-format request to the new action.
  2. Rails couldn't find a new.js.erb template to respond with, so it threw the UnknownFormat error.
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:06:00