Created
June 9, 2015 19:24
-
-
Save kalyco/9c5b3d6abe8322747075 to your computer and use it in GitHub Desktop.
Formatting rails data to a JSON through a serializer
This file contains 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
# a rails controller set up to send user information to ember's profile page | |
class UsersController < Devise::SessionsController | |
def show | |
@user = User.find(params[:user_id]) | |
respond_to do |format| | |
format.json { render json: @user } | |
end | |
end | |
end | |
# from here, ActiveModel Serializer picks it up. | |
# to make that work create a serializers- folder in app directory | |
# then create a _user_serializer.rb file that looks like this: | |
class UserSerializer < ActiveModel::Serializer | |
attributes: :first_name, :last_name | |
end | |
# And that's it! Rails is now an API for the Ember app! No more ERB! No more Haml! Forever JSON! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment