Separating Angularjs and Rails apps as standalone components

All we need is an easy explanation of the problem, so here it is.

I wanted to try out Angularjs. However, I have been trouble deciding on where I should located my angular app.

I am using Rails framework for the backend. I have seen tutorials where the entire angular app lives under the assets/javascript folder.

I was wondering if instead of living within the assets/javascript folder, I could make it live outside my rails directory entirely. That way, I can potentially separate my backend and front end entirely. (Is this recommended?).

I believe the asset pipeline also precompiles a lot of the assets. If I were to separate out the angularjs asset, would I need to precompile the assets somehow?

Thanks

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

I’ve been working through a similar set of questions. There are some good tools that allow you to integrate AngularJS directly into your rails asset pipeline, and they to me look good if you want just a little bit of Angular.

However, if you want a full Angular front-end, aka a single page web app, I think you’ll eventually be limited by compatibility and some of the tooling. I feel like the Rails gems won’t quite keep up with Angular, and so you’ll be into version conflicts. I’ve also seen more and more tooling for Angular as a standalone, and I very much like the ng-boilerplate project template. I also like much of the testing tooling such as karma, and I haven’t really sorted out a way to integrate karma with rails.

For that reason, I eventually decided that I’d keep the two separate. Initially, I did that through creating a rails application and a separate angular application (separate directories). I used ng-boilerplate as the framework for the angular end. I wrote a tutorial on that. This eventually got a bit frustrating, and I wrote some more thoughts on it, the main annoyance was that I had two git repositories and it was annoying to keep them in synch. It’s also sort of annoying working with an IDE across two directories. I’ve ended up shifting to rails and angular being in the same folder, and they seem to play nice, as each uses different directories within that project.

In this current structure, I’m using the grunt setup that came with ng-boilerplate to minify all the code, package it and also run karma unit testing. I haven’t yet nailed the end-to-end testing, but it’s on my list. I’ve found this to be a relatively productive work environment. My chosen structure for my pages, controllers and karma test cases has some repeated code (I’m choosing not to factor it out to maintain readability). I’m planning to extend the rails scaffold generator to create the javascript framework for me – so when I create a persons rails scaffold, it will also create a persons angularjs scaffold for me. I’ll update here if and when I do that work.

EDIT: I’ve completed the scaffolding work as well, which allows rails to automatically generate the angularJS elements when you generate the rails models/controllers etc. The blog post is here: http://technpol.wordpress.com/2013/09/24/rails-generator-to-generate-angular-views/

Method 2

You could use a grunt based workflow:

If you start with a decoupled frontend, use mocks at first so you can stay within angular and not lose focus switching between backend and frontend logic. An advantage of building a single page application is that you can develop it independently of the backend api. See (http://docs.angularjs.org/api/ngMockE2E.$httpBackend) for information about mocking http responses.

Method 3

We have been using AngularJS with our Rails application, in a way where we have been using Rails ERB templates, but switch over to using ng directive as and when required.

For this above setup we have used bower/bower-rails gem, which lets us use bower to manage the angular packages and their dependencies. We commit this into our repo, in the javascripts directory, and is taken care of by the Rails asset pipeline.

This setup has worked well for us considering we have above 50-50 % split of our views between the ERB templates and Angularjs.

More about this setup in the links below:

Method 4

There are many advantages of separating out your api service (rails in this case) and your frontend components. As we do for ios/android apps, angular client can live on its own as a separate entity. It will be a static website that can be deployed on s3 or any static website host. It just needs to communicate with your api service. You could setup CORS to make it possible.

Some advantages of this workflow

  • You could use rails-api, which is a subset of rails application. If you are just going to use rails to build apis, it doesnt make sense to have all functionality that a complete rails app provides. Its lightweight, faster and inclined more towards building API first arch than a MVC arch.
  • You could use yeoman angular-generator to generate an angular app and make the most of grunt & bower to manage build (concat,uglify,cdnify etc) and dependencies (angular modules).
  • Deployments will become flexible. You won’t need to depend on one to push the other.
  • If you ever plan to change your backend stack (eg rails to play/revel), you would not need to worry about your client components.
  • By splitting the development of the frontend and the Rails backend you could distribute the work over two development teams and keep the application as a whole very extensible.

There is also one downside to this approach.
By having the applications in two separate repositories, you can’t easily have a full integration test. So you will have to test the apps separately. You could mock your apis to test angular app.

We have been using this approach and would recommend others the same.
Less dependency & more productivity.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply