Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. render json: {'user': {name: 'Peter' }}
  2.  
  3. def index
  4. render json: @users, serializer: UserSerializer, adapter: :json_api
  5. end
  6.  
  7. class UsersController < ApplicationController
  8. before_action :set_user, only: [:show, :update, :destroy]
  9. ...
  10. ...
  11. # GET /users/1
  12. def show
  13. render json: @user
  14. end
  15. ...
  16. ...
  17. private
  18. # Use callbacks to share common setup or constraints between actions.
  19. def set_user
  20. @user = User.find(params[:id])
  21. end
  22.  
  23. def user_params
  24. ActiveModelSerializers::Deserialization.jsonapi_parse(params)
  25. end
  26. end
  27.  
  28. class UserSerializer < ActiveModel::Serializer
  29. attributes :id, :name, :email, :outro_atributo
  30.  
  31. belongs_to :alguma_associação
  32. has_many :outra_associação
  33. has_one :alguma_outra_associação
  34. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement