# Ember CLI Rails Ember CLI Rails is an integration story between (surprise surprise) Ember CLI and Rails 3.1 and up. It is designed to provide an easy way to organize your Rails backed Ember CLI application with a specific focus on upgradeability. Rails and Ember [slash Ember CLI] are maintained by different teams with different goals. As such, we believe that it is important to ensure smooth upgrading of both aspects of your application. A large contingent of Ember developers use Rails. And Rails is awesome. With the upcoming changes to Ember 2.0 and the Ember community's desire to unify around Ember CLI it is now more important than ever to ensure that Rails and Ember CLI can coexist and development still be fun! To this end we have created a minimum set of features (which we will outline below) to allow you keep your Rails workflow while minimizing the risk of upgrade pain with your Ember build tools. For example, end-to-end tests with frameworks like Cucumber should just work. You should still be able leverage the asset pipeline, and all the conveniences that Rails offers. And you should get all the new goodies like ES6 modules and Ember CLI addons too! Without further ado, let's get in there! ## Installation Firstly, you'll have to include the gem in your `Gemfile` and `bundle install` ```ruby gem "ember-cli-rails" ``` Then you'll want to configure your installation by adding an `ember.rb` initializer. There is a generator to guide you, run: ```shell rails generate ember-cli:init ``` This will generate an initializer that looks like the following: ```ruby EmberCLI.configure do |c| c.app :frontend end ``` ##### options - `app` - this represents the name of the Ember CLI application. - `build_timeout` - seconds to allow Ember to build the application before timing out - `path` - the path where your Ember CLI application is located. The default value is the name of your app in the Rails root. - `enable` - a lambda that accepts each request's path. The default value is a lambda that returns `true`. ```ruby EmberCLI.configure do |c| c.app :adminpanel # path is "/adminpanel" c.app :frontend, path: "/path/to/your/ember-cli-app/on/disk", enable: -> path { path.starts_with?("/app/") } end ``` Once you've updated your initializer to taste, install Ember CLI if it is not already installed, and use it to generate your Ember CLI app in the location/s specified in the initializer. For example: ```sh cd frontend ember init ``` You will also need to install the [ember-cli-rails-addon](https://github.com/rondale-sc/ember-cli-rails-addon). For each of your Ember CLI applications, run: ```sh npm install --save-dev ember-cli-rails-addon@0.0.13 ``` And that's it! You should now be able to start up your Rails server and see your Ember CLI app. ### Multiple Ember CLI apps In the initializer you may specify multiple Ember CLI apps, each of which can be referenced with the view helper independently. You'd accomplish this like so: ```ruby EmberCLI.configure do |c| c.app :frontend c.app :admin_panel, path: "/somewhere/else" end ``` ## Usage First, specify in your controller that you don't want to render the layout (since EmberCLI's `index.html` is a fully-formed HTML document): ```rb # app/controllers/application.rb class ApplicationController < ActionController::Base def index render layout: false end end ``` To render the EmberCLI generated `index.html` into the view, use the `include_ember_index_html` helper: ```erb <%= include_ember_index_html :frontend %> ``` To inject markup into page, pass in a block that accepts the `head`, and (optionally) the `body`: ```erb <%= include_ember_index_html :frontend do |head| %> <% head.append do %> <%= csrf_meta_tags %> <% end %> <% end %> ``` The asset paths will be replaced with asset pipeline generated paths. *NOTE* This helper **requires** that the `index.html` file exists. If you see `Errno::ENOENT` errors in development, your requests are timing out before EmberCLI finishes compiling the application. To prevent race conditions, increase your `build_timeout` to ensure that the build finishes before your request is processed. ### Rendering the EmberCLI generated JS and CSS In addition to rendering the EmberCLI generated `index.html`, you can inject the `