Skip to content

Instantly share code, notes, and snippets.

@xlozinguez
Created April 17, 2013 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xlozinguez/5406443 to your computer and use it in GitHub Desktop.
Save xlozinguez/5406443 to your computer and use it in GitHub Desktop.
Serializer example
class UserSerializer < ActiveModel::Serializer
attributes :id,
:email,
:name,
:username,
:location,
:description,
:avatar_url,
:trainer_id,
:created_at,
:updated_at
end
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