11

I'm using rails 4. But I am getting unknown format error. Do you guys have any help for me. Here is my controller:

class EntriesController < ApplicationController
  respond_to :json

  def index
    respond_with Entry.all
  end
end

enter image description here

user3366155
  • 452
  • 1
  • 3
  • 16

3 Answers3

33

Add this to your routes config

resources :entries, defaults: { format: 'json' }

JNN
  • 947
  • 10
  • 19
9
def index
  respond_to do |format|

    @entry = Entry.all

    format.html 
    format.json { render json: @entry }

  end
end

hope it will help you

matanco
  • 2,096
  • 1
  • 13
  • 20
  • 1
    :) while this may not have concerned OP, this was what I was looking for! (I wanted to return the regular html view as default, but respond to `.json` in a different way – Don Cheadle Feb 09 '15 at 15:57
  • 1
    This is very helpful! It just works! However I don't understand why does it work? – newguy Apr 28 '16 at 13:53
-2

Add :html to respond_to (respond_to :html, :json) or remove the respond_to call altogether.

Ahmad Sherif
  • 5,923
  • 3
  • 21
  • 27
  • 1
    in Rails 4.2.2, adding `respond_to` at Controller-level gives an error about how it has been moved to a `gem` – Don Cheadle Feb 09 '15 at 16:00