Rails and Ember app with some notes
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
app
bin
config
db
lib
log
public
spec
vendor/assets
.gitignore
.rspec
.rubocop.yml
Gemfile
Gemfile.lock
Guardfile
README.md
Rakefile
config.ru

README.md

Rails and Ember.js App

Some known best practices

Use nested module/class definitions instead of compact style.

Preferred:

# app/controllers/api/v1/leads_controller.rb
module Api
  module V1
    class LeadsController < ApplicationController
      respond_to :json
    end
  end
end

Not preferred:

# app/controllers/api/v1/leads_controller.rb
class Api::V1::LeadsController < ApplicationController
  respond_to :json
  # methods
end

The routes should look like:

# config/routes.rb

namespace :api, defaults: { format: 'json' } do
  scope module: :v1 do
    resources :leads
  end
end

If you're not going to use modules, it would look like:

# config/routes.rb

namespace :api do
  namespace :v1 do
    resources :leads
  end
end

A JSON API document should be identified by the media type application/vnd.api+json

We achieve this by autoloading a module:

# lib/api_constraints.rb 
class ApiConstraints
  def initialize(options)
    @version = options[:version]
    @default = options[:default]
  end

  def matches?(req)
    @default || req.headers['Accept'].include?("application/vnd.myapp.v#{@version}")
  end
end

And updating routes to use this module:

namespace :api, defaults: { format: 'json' } do
  scope module: :v1, constraints: ApiConstraints.new(version: 1) do
    resources :leads
  end
end

Making this work with Ember.js is simple. Simply include headers on app/assets/javascripts/store.js.coffee

DS.RESTAdapter.reopen
  namespace: 'api'
  headers: {
    "Accept": "application/vnd.myapp.v1+json"
  }

Resources