Last active
August 29, 2015 14:02
-
-
Save brandoncordell/4ba5fc8c1c5b78c251d5 to your computer and use it in GitHub Desktop.
Returning json in Rails 4 using MongoMapper
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
# 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