Skip to content

Instantly share code, notes, and snippets.

@theRealNG
Last active August 29, 2015 14:20
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 theRealNG/de9a91fcd7e9806f5c2f to your computer and use it in GitHub Desktop.
Save theRealNG/de9a91fcd7e9806f5c2f to your computer and use it in GitHub Desktop.
User using as_json vs active_model_serialization
def show
respond_with User.find(params[:id])
end
def create
user = User.new(user_params)
if user.save
render json: user, status: :created, location: [:api, user]
else
render json: { errors: user.errors }, status: :unprocessable_entity
end
end
def update
user = current_user
if user.update_attributes(user_params)
render json: user, status: :ok, location: [:api, user]
else
render json: { errors: user.errors }, status: :unprocessable_entity
end
end
def show
user = User.find(params[:id])
render json: user.as_json(only: [:id,:email])
end
def create
user = User.new(user_params)
if user.save
render json: user.as_json(only: [:id,:email]), status: :created, location: [:api, user]
else
render json: { errors: user.errors }, status: :unprocessable_entity
end
end
def update
user = current_user
if user.update_attributes(user_params)
render json: user.as_json(only: [:id,:email]), status: :ok, location: [:api, user]
else
render json: { errors: user.errors }, status: :unprocessable_entity
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment