Skip to content

Instantly share code, notes, and snippets.

@brandoncordell
Last active August 29, 2015 14:02
Show Gist options
  • Save brandoncordell/4ba5fc8c1c5b78c251d5 to your computer and use it in GitHub Desktop.
Save brandoncordell/4ba5fc8c1c5b78c251d5 to your computer and use it in GitHub Desktop.
Returning json in Rails 4 using MongoMapper
# Model
class Job
include MongoMapper::Document
key :title, String
key :description, String
end
# Controller
class Api::V1::JobsController < ApplicationController
respond_to :json
def index
respond_with Job.all
end
end
# Visiting /api/v1/jobs.json I get the following json
# {"jobs":[{"jobs":{"description":"Another Test!","id":"53963d2189fd48c1b2000001","title":"Test job!"}}]}
##
# Working Example
##
class Api::V1::JobsController < ActionController
...
def index
respond_with jobs: Job.all
end
...
end
# {"jobs":[{"description":"Another Test!","id":"53963d2189fd48c1b2000001","title":"Test job!"}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment