在我的rails应用程序中,我向服务器发出了ajax请求,以存储一些数据。以前可以正常工作,但是现在出现错误:
ActionController::UnknownFormat (ActionController::UnknownFormat): app/controllers/reservations_controller.rb:45:in `create'
如下是控制器和我声明数据类型为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
function.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操作,如下所示:
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的方法,但ANOT指定 格式 ,其中new呈现页面。因此,错误ActionController::UnknownFormat。
respond_to
new
ActionController::UnknownFormat