ActionController :: UnknownFormat

In my rails app, I have an ajax request to the server to save some data. This worked without issue, but now I am getting the error:

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

      

As the next controller and my javascript file where I declare the datatype should 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
      })

      

Complete error log:

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)

      

+29


source to share


5 answers


Update the action create

as shown 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 a method respond_to

but anot defining the formatin which the page is displayed new

. Hence the error ActionController::UnknownFormat

.

+38


source


You can also modify the config / routes.rb file, for example:

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

      

Which json format will be set by default. It works great for me in Rails 4.



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

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

      

with the above everything under /api

will be formatted as json by default.

+19


source


Ok, I love this post because I have the same error. So I added the top line like in your reply_to: html controller: json

then i got another error (see below)

Reference level respond_to' feature has been extracted to the

. Add it to your Gemfile to continue using this feature: gem "responseers", "~> 2.0". See the Rails Upgrade Guide for details. But it had nothing to do with it.

+1


source


This issue happened to me and referred to just add

 respond_to :html, :json

      

to file ApplicationController

You can check development issues on Github: https://github.com/plataformatec/devise/issues/2667

+1


source


There is another scenario where this problem is reproducible (as in my case). When the CUSTOMER REQUEST does not contain the correct extension on the URL, the controller cannot determine the desired result format.

For example: controller is set to respond_to :json

(as the only option with no HTML response) - while client call is set to /reservations

instead /reservations.json

.

On the bottom line, change the client call to /reservations.json

.

+1


source







All Articles