Rails 5 on aws c9

N5-1. post 결과물을 json으로 응답받기

컴퍼 2020. 7. 5. 16:43
class PostsController < ApplicationController
  ...
  ***respond_to :html, :json***

  def index
    ...
    ***respond_with(@posts)***
  end
  ...

end

삽입


The best way to do this is just like Amitkumar Jha said, but if you need a simple and quick way to render your objects, you can also use this "shortcut":

def index
  @users = User.all
  respond_to :html, :json, :xml
end

Or make respond_to work for all the actions in the controller using respond_with :

class UserController < ApplicationController
  respond_to :html, :json, :xml

  def index
    @users = User.all
    respond_with(@users)
  end
end

출처 : https://stackoverflow.com/questions/20188047/rails-respond-to-json-and-html