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

2020. 7. 5. 16:43Rails 5 on aws c9

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

'Rails 5 on aws c9' 카테고리의 다른 글

N7. N:N관계  (0) 2020.07.05
N6. Post 수정과 삭제  (0) 2020.07.05
N5. Post 리스트와 생성  (0) 2020.07.05
N4. model relation  (0) 2020.07.05
N3. Devise 적용하기  (0) 2020.07.05