Skip to content

Instantly share code, notes, and snippets.

@kalyco
Created June 9, 2015 19:24
Show Gist options
  • Save kalyco/9c5b3d6abe8322747075 to your computer and use it in GitHub Desktop.
Save kalyco/9c5b3d6abe8322747075 to your computer and use it in GitHub Desktop.
Formatting rails data to a JSON through a serializer
# 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