How to Wire Up Ruby on Rails and AngularJS as a Single-Page Application
angularonrails.com
angularonrails.com
We loved the combination of Rails and AngularJS, but we didn't like that we had to write a lot of stuff twice (ie. Rails routes & Angular routes, Rails controllers & Angular controllers). Therefore we have written quite some glue code to automatically generate AngularJS code from our Rails code. I have been wanting to make a full write-up of all our experience in combining Angular & Rails for a while now, but didn't get to it yet. I did write-up a quick and dirty way to automatically generate Angular Resources from you Rails controllers, though. (http://10kb.nl/blog/automatically-generate-angular-resources...)
And, if anyone has any questions about combining Rails & Angular feel free to shoot me an e-mail.
[0] http://mbell697.github.io/2014/02/04/yeoman-angular-rails-ht...
https://github.com/lynndylanhurley/ng-token-auth https://github.com/lynndylanhurley/devise_token_auth
I have to say though, Rails used to be known for "convention over configuration", but when I've tried to use it lately I've found it becomes almost as complicated as configuring enterprise Java projects.
https://github.com/cyu/rack-cors
I used that gem recently, and it fixed my issue. Just added the dev, prod hostnames explicitly in the origins list in my application.rb.
Having run such things on entirely different servers, never mind localhost ports, what I needed to do was set the Access-Control-Allow-Origin http header in the rails controller (same principle applies to other frameworks). One simple guide is here: http://madhukaudantha.blogspot.co.uk/2011/05/access-control-...
Slightly ruined by that dreadful blogger interface, but there you go.
Hopefully it helps you!
>I've found it becomes almost as complicated as configuring enterprise Java projects.
Do you have any specific examples of where you were having troubles?
He's using grunt-connect-proxy to proxy the API requests through the grunt server so there are no CORS requests in this setup.
http://thomasleecopeland.com/2014/03/12/doing-a-rails-angula...
Group.query().then(function(groups){
$scope.groups = groups;
})It looks like the docs still suggest the method used in the blog post: https://docs.angularjs.org/api/ngResource/service/$resource
"It is important to realize that invoking a $resource object method immediately returns an empty reference [...] Once the data is returned from the server the existing reference is populated with the actual data. This is a useful trick since usually the resource is assigned to a model which is then rendered by the view. [...] This means that in most cases one never has to write a callback function for the action methods."
The $resource usage described still works in version 1.2.19, with no promise unwrapping options set. I could be wrong, but I think it is because the $resource returns a reference to an empty object that has values copied into it when the call completes. The actual promise is a property named $promise on the returned object.
Example: http://jsfiddle.net/92zmG/7/
For json serialization you may want to look at Active Model Serializers[0], I found it much easier to work with than rabl, and it's also written by the rails-api group.
That said, you're correct, AMS <-> RABL is the right comparison, not Rails-API <-> RABL.
Most setups I've seen just let Rails serve the templates.
Also grunt has a watch process that see code changes, it will then compile them and have the server get the browser to reload content to get the new version.
When this gets deployed to your production server, you will probably use the same server process that handles the Rails requests. grunt is for development only.