Created
April 17, 2013 18:08
-
-
Save xlozinguez/5406443 to your computer and use it in GitHub Desktop.
Serializer example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserSerializer < ActiveModel::Serializer | |
attributes :id, | |
:email, | |
:name, | |
:username, | |
:location, | |
:description, | |
:avatar_url, | |
:trainer_id, | |
:created_at, | |
:updated_at | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Api::UsersController < Api::ApiController | |
before_filter :user_admin?, only: :index | |
before_filter :find_user, only: [:show, :update] | |
def index | |
render json: User.all, each_serializer: UserSerializer | |
end | |
def show | |
render json: @user, serializer: UserSerializer, root: :user | |
end | |
def update | |
if @user.update_attributes(params[:user]) | |
return render status: 200, json: @user, serializer: UserSerializer, root: :user | |
else | |
return render status: 422, json: {success: false, errors: @user.errors.full_messages.map{|error|{error: error}}} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment