我已經讀過無數關於此問題的SO問題,並且找不到解決方案。MeetingsController中的ActionController :: UnknownFormat#update
我有一個按鈕,在這裏更新會議:
<%= form_for(meeting) do |f| %>
<%= f.hidden_field :accepted, value: true %>
<%= button_tag(type: 'submit', class: "btn_primary") do %>
Accept <svg><use xlink:href="#checkmark"/></svg>
<% end %>
<% end %>
我得到一個未知格式錯誤,但它仍然更新。什麼導致了這個錯誤?
我試着從更新方法中刪除respond_to do |format|
。它解決了這個按鈕的問題,但隨後打破了平臺上調用會議/更新的所有其他按鈕。
class MeetingsController < ApplicationController
respond_to :json, :html
def update
respond_to do |format| **error highlights this line**
if @meeting.update(meeting_params)
format.json { render :show, status: :ok, location: @meeting }
else
format.html { render :edit }
format.json { render json: @meeting.errors, status: :unprocessable_entity }
end
end
end
end
控制檯:
Started PATCH "/meetings/224" for 127.0.0.1 at 2016-11-11 16:05:27 -0500
Processing by MeetingsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"hK6AoOZuw9DWyKUXw1dXWOSUolooWgBUPnkItUJX5Tm7XvimsHd9518pkqwVvNhUi3L3vlA4OZaJZiAgrbS0Ig==", "meeting"=>{"accepted"=>"true"}, "button"=>"", "id"=>"224"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Meeting Load (0.2ms) SELECT "meetings".* FROM "meetings" WHERE "meetings"."id" = ? LIMIT 1 [["id", 224]]
(0.3ms) begin transaction
SQL (0.5ms) UPDATE "meetings" SET "accepted" = ?, "updated_at" = ? WHERE "meetings"."id" = ? [["accepted", "t"], ["updated_at", "2016-11-11 21:05:27.882521"], ["id", 224]]
(1.3ms) commit transaction
Completed 406 Not Acceptable in 24ms (ActiveRecord: 2.7ms)
ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/meetings_controller.rb:65:in `update'
這是從來沒有一個問題,貌似出來沒有在那裏。這個錯誤的原因是什麼?謝謝!
好的,明白了。這固定它,但只是爲了澄清原因,所以我知道未來 - 每次需要爲html和json指定方向時,即使我不一定需要重定向,也是如此。謝謝你的幫助!! – gwalshington
@ gwalshington,但你確實需要重定向,所以應用程序知道重新加載要做什麼。即使它只是redirect_to:後退。如果你不希望頁面重新加載......你需要使用ajax提交。 – toddmetheny
如果您想要在更新後呈現網頁...您需要format.html調用。 – toddmetheny