Last active
August 29, 2015 14:20
-
-
Save theRealNG/de9a91fcd7e9806f5c2f to your computer and use it in GitHub Desktop.
User using as_json vs active_model_serialization
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
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 |
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
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