weixin_33717117 2014-04-08 17:15 采纳率: 0%
浏览 71

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)
  • 写回答

5条回答 默认 最新

  • 胖鸭 2014-04-08 18:00
    关注

    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 anot specifying the format in which a new page is rendered. Hence, the error ActionController::UnknownFormat .

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效