# ember-rails [![Build Status](https://secure.travis-ci.org/emberjs/ember-rails.png?branch=master)](http://travis-ci.org/emberjs/ember-rails) [![Dependency Status](https://gemnasium.com/emberjs/ember-rails.png)](https://gemnasium.com/emberjs/ember-rails) ember-rails makes developing an [Ember.JS](http://emberjs.com/) application much easier in Rails 3.1+. The following functionalities are included in this gem: - Pre-compiling of your handlebars templates when building your asset pipeline. - Includes development and production copies of Ember, [Ember Data](https://github.com/emberjs/data) and [Handlebars](https://github.com/wycats/handlebars.js). - Includes [ActiveModel::Serializer](https://github.com/rails-api/active_model_serializers) for integration with Ember Data. You can see an example of how to use the gem [here](https://github.com/keithpitt/ember-rails-example). There is also a great tutorial by [Dan Gebhardt](https://twitter.com/#!/dgeb) called "[Beginning Ember.js on Rails](http://www.cerebris.com/blog/2012/01/24/beginning-ember-js-on-rails-part-1/)" which is a great read if you're just starting out with Rails and Ember.js ## Getting started * Add the gem to your application Gemfile: ```ruby gem 'ember-rails' gem 'ember-source', '1.2.0' # or the version you need ``` * Run `bundle install` * Next, generate the application structure: ```shell rails generate ember:bootstrap ``` * Restart your server (if it's running) Notes: To install the latest builds of ember and ember-data. It should be noted that the examples in the [getting started guide](http://emberjs.com/guides/getting-started/) have been designed to use the released version of ember: ```shell rails generate ember:install ``` You'll probably need to clear out your cache after doing this with ```shell rake tmp:clear ``` ## For CoffeeScript support 1. Add coffee-rails to the Gemfile ```ruby gem 'coffee-rails' ``` 2. Run the bootstrap generator in step 4 with an extra flag instead: ```sh rails g ember:bootstrap -g --javascript-engine coffee ``` Note: Ember-rails include some flags options for bootstrap generator: ``` --ember-path or -d # custom ember path --skip-git or -g # skip git keeps --javascript-engine # engine for javascript (js or coffee) --app-name or -n # custom ember app name ``` ## For EmberScript support [EmberScript](http://www.emberscript.com) is a dialect of CoffeeScript with extra support for computed properties (which do not have to be explicitly declared), the `class` / `extends` syntax, and extra syntax to support observers and mixins. To get EmberScript support, make sure you have the following in your Gemfile: ```ruby gem 'ember_script-rails', :github => 'ghempton/ember-script-rails' ``` You can now use the flag `--javascript-engine=em` to specify EmberScript assets in your generators, but all of the generators will default to using an EmberScript variant first. ## Configuration Options The following options are availabe for configuration in your application or environment level config files (`config/application.rb`, `config/environments/development.rb`, etc.): * `config.ember.variant` - Used to determine which Ember variant to use. Valid options: `:development`, `:production`. * `config.ember.app_name` - Used to specify a default application name for all generators. * `config.ember.ember_path` - Used to specify a default custom root path for all generators. * `config.handlebars.precompile` - Used to enable or disable precompilation. Default value: `true`. * `config.handlebars.templates_root` - Set the root path (under `app/assets/javascripts`) for templates to be looked up in. Default value: `"templates"`. * `config.handlebars.templates_path_separator` - The path separator to use for templates. Default value: `'/'`. * `config.handlebars.output_type` - Configures the style of output (options are `:amd` and `:global`). Default value: `:global`. ## Architecture Ember does not require an organized file structure. However, ember-rails allows you to use `rails g ember:bootstrap` to create the following directory structure under `app/assets/javascripts`: controllers/ helpers/ components/ models/ routes/ templates/ templates/components views/ Additionally, it will add the following lines to `app/assets/javascripts/application.js`. By default, it uses the Rails Application's name and creates an `rails_app_name.js` file to setup application namespace and initial requires: //= require handlebars //= require ember //= require ember-data //= require_self //= require rails_app_name RailsAppName = Ember.Application.create(); *Example:* rails g ember:bootstrap insert app/assets/javascripts/application.js create app/assets/javascripts/models create app/assets/javascripts/models/.gitkeep create app/assets/javascripts/controllers create app/assets/javascripts/controllers/.gitkeep create app/assets/javascripts/views create app/assets/javascripts/views/.gitkeep create app/assets/javascripts/helpers create app/assets/javascripts/helpers/.gitkeep create app/assets/javascripts/components create app/assets/javascripts/components/.gitkeep create app/assets/javascripts/templates create app/assets/javascripts/templates/.gitkeep create app/assets/javascripts/templates/components create app/assets/javascripts/templates/components/.gitkeep create app/assets/javascripts/app.js If you want to avoid `.gitkeep` files, use the `skip git` option like this: `rails g ember:bootstrap -g`. Ask Rails to serve HandlebarsJS and pre-compile templates to Ember by putting each template in a dedicated ".js.hjs", ".hbs" or ".handlebars" file (e.g. `app/assets/javascripts/templates/admin_panel.handlebars`) and including the assets in your layout: <%= javascript_include_tag "templates/admin_panel" %> If you want to strip template root from template names, add `templates_root` option to your application configuration block. By default, `templates_root` is `'templates'`. config.handlebars.templates_root = 'ember_templates' If you store templates in a file like `app/assets/javascripts/ember_templates/admin_panel.handlebars` after setting the above config, it will be made available to Ember as the `admin_panel` template. _(Note: you must clear the local sprockets cache after modifying `templates_root`, stored by default in `tmp/cache/assets`)_ Default behavior for ember-rails is to precompile handlebars templates. If you don't want this behavior you can turn it off in your application configuration (or per environment in: `config/environments/development.rb`) block: config.handlebars.precompile = false _(Note: you must clear the local sprockets cache if you disable precompilation, stored by default in `tmp/cache/assets`)_ Bundle all templates together thanks to Sprockets, e.g create `app/assets/javascripts/templates/all.js` with: //= require_tree . Now a single line in the layout loads everything: <%= javascript_include_tag "templates/all" %> If you use Slim or Haml templates, you can use handlebars filter : handlebars: It will be translated as : ### Note about ember components When necessary, ember-rails adheres to a conventional folder structure. To create an ember component you must define the handlebars file *inside* the *components* folder under the templates folder of your project to properly register your handlebars component file. *Example* With the following folder structure: components/ controllers/ helpers/ models/ routes/ templates/ components/ my-component.handlebars views/ and a *my-component.handlebars* file with the following contents