ActionController::UnknownFormat
Asked Answered
L

7

58

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)
Libretto answered 8/4, 2014 at 17:15 Comment(1)
Can you share the complete server log when you get this error?Curry
C
62

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 .

Curry answered 8/4, 2014 at 18:0 Comment(3)
exactly, apparently he's not saving on the database (the if @reservation.save returns false) but I no longer get the errorLibretto
Yup..you would get the error only when @reservation.save fails as in that case else part is executed where the format is missing.Curry
You DO NOT need to specify format.html or format.json UNLESS you invoke render within a respond_to block!Lackaday
G
33

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

 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.

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 /api will be formatted as json by default.

Gyp answered 26/2, 2015 at 19:14 Comment(0)
E
7

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.

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 /reservations instead of /reservations.json.

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

Empire answered 1/4, 2018 at 5:3 Comment(0)
J
4

This problem happened with me and sovled by just add

 respond_to :html, :json

to ApplicationController file

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

Jeanejeanelle answered 18/6, 2017 at 12:47 Comment(3)
That's not a method the ApplicationController understands.Sled
@Sled do you mean "respond_to" ? you can check github issues link attached.Jeanejeanelle
Indeed. Perhaps there's some reason to do with rails 5 that it wasn't a recognised method in that context for me.Sled
L
1

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

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.

Leta answered 4/2, 2015 at 2:14 Comment(1)
It was the missing script tags in my layout.Leta
A
0

I got this error when trying to render an XML response - I had to change my template name from index.html.erb to index.xml.erb and then it worked.

Alternant answered 21/3, 2021 at 6:29 Comment(0)
T
0

mine was fixed because i didnt have / / in the scope, i had this (getting error for json):

Rails.application.routes.draw do
  scope '(:base_url)', base_url: #{ENV.fetch('BASE_URL').to_s} do
    ...
  end
end

instead of this (fixed):

Rails.application.routes.draw do
  scope '(:base_url)', base_url: /#{ENV.fetch('BASE_URL').to_s}/ do
    ...
  end
end
Teal answered 9/4, 2022 at 1:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.