Specify The Rails API Host for Ember Production Deployment

December 31, 2019

I’m not going to go through ALL the steps to deploy an Ember and Rails app to production (yet), but wanted to quickly cover some of the Ember steps I needed to take in order to deploy the Beverage app we made in my previous post.

In development using the terminal we would pass in the --proxy flag to ember s which tells Ember where to fetch data from, but in productions we aren’t using ember s so we need to add the location of our Rails API to the config/environment.js file. Find the ‘production’ section and add an environment variable for storing the url to your Rails API.

//config/environment.js
//...

if (environment === 'production') {
  // here you can enable a production-specific feature
  ENV.APP.host = 'https://api.beverage.blake.app'
}

//...

Now we have to tell Ember Data how to access this variable. Because we are using JSON API (the default adapter) for Ember Data we don’t actually have a place for this yet and we need to override the default by creating an adapter.

mkdir app/adapters
touch app/adapters/application.js

In order to use our environment variable we need to import it, then we can set our host variable.

// app/adapters/application.js
import DS from 'ember-data';
import ENV from 'ember-form/config/environment';

const { JSONAPIAdapter } = DS;

export default class ApplicationAdapter extends JSONAPIAdapter {
  host = ENV.APP.host;
}

Now when you run ember build --prod it will contain the host for your Rails API, and in development you can continue to use the --proxy flag with ember s.