gpt4 book ai didi

ruby - Action Controller ::未知格式

转载 作者:数据小太阳 更新时间:2023-10-29 06:22:45 26 4
gpt4 key购买 nike

在我的 Rails 应用程序中,我向服务器发出了一个 ajax 请求,以存储一些数据。这曾经没有任何问题,但现在我得到一个错误:

ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/reservations_controller.rb:45:in `create'

如下是 Controller 和我声明数据类型为 JSON 的 javascript 文件

class ReservationController < ApplicationController

respond_to :html, :json

def create
...
respond_to do |format|
if @reservation.save
format.html do
redirect_to '/'
end
format.json { render json: @reservation.to_json }
else
render 'new'
end
end # respond_to
end # create
end # ReservationController

函数.js

$.ajax({
url: url_link,
dataType: 'json',
type: 'POST',
data: dataToSend
})

完整的错误日志为:

Completed 406 Not Acceptable in 45ms

ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/bookings_controller.rb:45:in `create'

Rendered /Users/tiagovieira/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.5ms)
Rendered /Users/tiagovieira/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.8ms)
Rendered /Users/tiagovieira/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.8ms)
Rendered /Users/tiagovieira/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (9.6ms)

最佳答案

更新 create 操作如下:

def create
...
respond_to do |format|
if @reservation.save
format.html do
redirect_to '/'
end
format.json { render json: @reservation.to_json }
else
format.html { render 'new'} ## Specify the format in which you are rendering "new" page
format.json { render json: @reservation.errors } ## You might want to specify a json format as well
end
end
end

您正在使用 respond_to 方法,但未指定呈现 页面的格式。因此,错误 ActionController::UnknownFormat

关于ruby - Action Controller ::未知格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22943892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com