Unknown format in rails 4

2020-02-10 18:06发布

问题:

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

回答1:

Add this to your routes config

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



回答2:

def index
  respond_to do |format|

    @entry = Entry.all

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

  end
end

hope it will help you



回答3:

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