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

1
  • 1
    But its working perfectly on rails 3 only problem in rails 4 May 11, 2014 at 8:35

3 Answers 3

33

Add this to your routes config

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

1
  • 1
    @user3366155 glad it helped. would you mind choosing my answer :)
    – JNN
    May 21, 2014 at 7:05
9
def index
  respond_to do |format|

    @entry = Entry.all

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

  end
end

hope it will help you

2
  • 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 Feb 9, 2015 at 15:57
  • 1
    This is very helpful! It just works! However I don't understand why does it work?
    – newguy
    Apr 28, 2016 at 13:53
-2

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

1
  • 1
    in Rails 4.2.2, adding respond_to at Controller-level gives an error about how it has been moved to a gem Feb 9, 2015 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.