Created
June 5, 2014 17:56
-
-
Save joshsmith/dec2d44907956305003a to your computer and use it in GitHub Desktop.
Serializers
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
class RecipeSerializer < ActiveModel::Serializer | |
embed :ids, include: true | |
has_many :steps | |
attributes :id, :title | |
end |
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
class RecipesController < ApplicationController | |
def index | |
@recipes = Recipe.all | |
render json: @recipes, each_serializer: ShortRecipeSerializer | |
end | |
def show | |
@recipe = Recipe.find(params[:id]) | |
render json: @recipe | |
end | |
end |
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
class ShortRecipeSerializer < ActiveModel::Serializer | |
attributes :id, :title | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment