ActionController的:: UnknownFormat [英] ActionController::UnknownFormat
本文介绍了ActionController的:: UnknownFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的Rails应用我有一个AJAX请求到服务器,存储一些数据。这种使用没有任何问题的工作,但现在我得到一个错误:
的ActionController :: UnknownFormat(ActionController的:: UnknownFormat):
应用程序/控制器/ reservations_controller.rb:45:在'创造'
由于以下是控制器和我的JavaScript文件,其中我宣布的数据类型确实是JSON
类ReservationController<的ApplicationController
respond_to代码:HTML,:JSON
DEF创建
...
respond_to代码做|格式|
如果@ reservation.save
的format.html做
redirect_to时'/'
结束
format.json {渲染JSON:@ reservation.to_json}
其他
使新
结束
结束#的respond_to
结束#创建
结束#ReservationController
function.js
$。阿贾克斯({
网址:URL_LINK,
数据类型:JSON,
键入:POST,
数据:dataToSend
})
完整的错误日志:
已完成406不可接受的45ms
ActionController的:: UnknownFormat(ActionController的:: UnknownFormat):
应用程序/控制器/ bookings_controller.rb:45:在'创造'
渲染/Users/tiagovieira/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.5毫秒)
渲染/Users/tiagovieira/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (为0.8ms)
渲染/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)
渲染/Users/tiagovieira/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb救援内/布局(9.6ms)
解决方案
更新创建
如下操作:
高清创建
...
respond_to代码做|格式|
如果@ reservation.save
的format.html做
redirect_to时'/'
结束
format.json {渲染JSON:@ reservation.to_json}
其他
的format.html {呈现'新'} ##指定要在其中呈现的格式为新页面
format.json {渲染JSON:@ reservation.errors} ##,您可能需要指定一个JSON格式以及
结束
结束
结束
正在使用的respond_to
的方法,但没有指定格式,其中新
页面的呈现方式。因此,误差的ActionController :: UnknownFormat
。
In my rails app I have a ajax request to the server, to store some data. This used to work without any problem, but now I get an error:
ActionController::UnknownFormat (ActionController::UnknownFormat):
app/controllers/reservations_controller.rb:45:in `create'
As following is the controller and my javascript file where I declare the datatype do be JSON
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
})
The complete error log is:
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)
解决方案
Update the create
action as below:
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
You are using respond_to
method but are not specifying the format in which a new
page is rendered. Hence, the error ActionController::UnknownFormat
.
这篇关于ActionController的:: UnknownFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文