Last Updated: February 25, 2016
·
1.195K
· benburton

Put EmberJS controllers/models/views in Rails' app/* folders

Recently I've been playing around with EmberJS and a Rails API application. The initial setup of the tree for models/views/controllers looked something like this:

.
├── Gemfile
├── Gemfile.lock
├── README.md
├── Rakefile
└── app
    ├── assets
    │   ├── javascripts
    │   │   ├── controllers
    │   │   ├── helpers
    │   │   ├── routes
    │   │   ├── templates
    │   │   ├── views
    ├── controllers
    ├── models
    └── views
        └── index.html.haml

From my perspective, the subtrees of app/assets/javascripts and app/ looked a pretty similar. I thought it might be neat to have the javascript files representing the model on the client in the same directory as the models on the server. So instead of:

.
└── app
    ├── assets
    │   └── javascripts
    │       └── models
    |           └── user.js
    └── models
        └── user.rb

It might be interesting to have this scructure:

.
└── app        
    └── models
        ├── user.rb
        └── user.js

I'm not advocating that this is a good idea... but I thought it might be interesting to see if it were possible. It turns out that all you have to do is tell Sprockets how to look in the appropriate subdirectories of app by adding this to application.rb:

module MyApplication
  class Application < Rails::Application
    config.assets.paths += %w(models views controllers).map{|p| "#{Rails.root}/app/#{p}"}
  end
end

and then tell the sprocket manifest file to look for your files in application.js:

//= require user

1 Response
Add your response

I like this idea. Do you think there are any specific downsides to structuring in such a way?

over 1 year ago ·