Using AngularJS with Rails 4 4/2/14
Using AngularJS with Rails 4 should be pretty easy. Include the AngularJS library in your asset pipeline and start coding away. Unfortunately, it’s not quite that simple. You may have trouble…
Compressing AngularJS code in your asset pipeline
By default, Rails 4 compresses all of your JavaScript using UglifyJS. Sadly, UglifyJS clobbers AngularJS files, but the fix is a snap1. Go into config/environments/production.rb
find the config.assets.js_compressor
line and tell Rails to run the Uglifier without the mangle
option:
Processing Angular GET requests
To increase security, Rails will only process GET (and other form requests) if they pass along the proper cross-site request forgery (CSRF) token2. Rails is also particular about the Accept
header when returning JSON. You can solve both issues by configuring your app’s global $httpProvider
:
In your AngularJS $http
GET calls, you’ll also need to pass along a specific Content-Type
header:
Playing nice with Turbolinks
If you’re going to leave Turbolinks enabled, you’ll need a way of re-bootstrapping your app after the new page content is loaded in3. Turbolinks has an event for this named page:load
we can bind to after we define our Angular app:
Now that we’re bootstrapping our app in our JavaScript, we need to remove the ng-app
bootstrap directive from our HTML. So this:
Can just be:
To be continued
I’ll be sure to update this post as I learn more about using AngularJS with Rails 4.
-
Big thanks to Beryllium Work for the fix. ↩
-
If you’re using jQuery, Rails built-in helper will do this automatically. ↩
-
Thanks for fiedl and skalb on StackOverflow for the Turbolinks fix. ↩