ruby ActionController::UnknownFormat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22943892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:26:12  来源:igfitidea点击:

ActionController::UnknownFormat

rubyajaxruby-on-rails-4respond-to

提问by tvieira

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:

在我的 rails 应用程序中,我向服务器发送了一个 ajax 请求,以存储一些数据。这曾经可以正常工作,但现在出现错误:

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

以下是控制器和我的 javascript 文件,我在其中声明数据类型确实是 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

函数.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)

回答by Kirti Thorat

Update the createaction as below:

更新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

You are using respond_tomethod but anot specifying the formatin which a newpage is rendered. Hence, the error ActionController::UnknownFormat.

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

回答by BPH

You can also modify your config/routes.rb file like:

您还可以修改您的 config/routes.rb 文件,例如:

 get 'ajax/:action', to: 'ajax#:action', :defaults => { :format => 'json' }

Which will default the format to json. It is working fine for me in Rails 4.

这将默认格式为json。它在 Rails 4 中对我来说很好用。

Or if you want to go even further and you are using namespaces, you can cut down the duplicates:

或者,如果您想更进一步并且正在使用名称空间,则可以减少重复项:

namespace :api, defaults: {format: 'json'} do
   #your controller routes here ...
end

with the above everything under /apiwill be formatted as json by default.

上面的所有内容/api默认都将被格式化为json。

回答by Abdallah Okasha

This problem happened with me and sovled by just add

这个问题发生在我身上,只需添加即可解决

 respond_to :html, :json

to ApplicationController file

到 ApplicationController 文件

You can Check Devise issues on Github: https://github.com/plataformatec/devise/issues/2667

你可以在 Github 上查看设计问题:https: //github.com/plataformatec/devise/issues/2667

回答by orberkov

There is another scenario where this issue reproduces (as in my case). When THE CLIENT REQUEST doesn't contain the right extension on the url, the controller can't identify the desired result format.

还有另一种情况会重现此问题(如我的情况)。当客户端请求在 url 上不包含正确的扩展名时,控制器无法识别所需的结果格式。

For example: the controller is set to respond_to :json(as a single option, without a HTML response)- while the client call is set to /reservationsinstead of /reservations.json.

例如:控制器设置为respond_to :json(作为单个选项,没有 HTML 响应)- 而客户端调用设置为/reservations而不是/reservations.json

Bottom line, change the client call to /reservations.json.

最重要的是,将客户端调用更改为/reservations.json.

回答by Douglas G. Allen

Well I fond this post because I got a similar error. So I added the top line like in your controller respond_to :html, :json

好吧,我喜欢这篇文章,因为我遇到了类似的错误。所以我在你的控制器 respond_to :html, :json 中添加了第一行

then I got a different error(see below)

然后我得到了一个不同的错误(见下文)

The controller-level respond_to' feature has been extracted to theresponders` gem. Add it to your Gemfile to continue using this feature: gem 'responders', '~> 2.0' Consult the Rails upgrade guide for details. But that had nothing to do with it.

控制器级respond_to' feature has been extracted to the响应者的 gem。将它添加到您的 Gemfile 以继续使用此功能: gem 'responders', '~> 2.0' 有关详细信息,请参阅 Rails 升级指南。但这与它无关。