0

I'm about to start a new project and I am unsure if using AngularJS for my front end would be a good idea not. I've read about people saying this isn't the smartest way of doing a project. And even if I did, Is there a way to get AngularJS to interact with the controllers? This question may be redundant but I am actually curious of how to effectively do this without it being a waste of time.

3 Answers 3

1

I've never completely done it, but I believe the way to go is to build a Rails api and then have a separate Angular project use said api. The api could also be used to build a mobile app. I think the Angular project would still need to be served from a Node.js server in production, but I don't think that would be a big deal.

This is what I used to learn how to build a Rails api: http://apionrails.icalialabs.com/book/chapter_one

You can do it within an existing project and share the models from it.

Sign up to request clarification or add additional context in comments.

1 Comment

The distributable "product" of AngularJS is just HTML and JavaScript (and other static assets), so it can be served up statically. It can be served up from the existing Rails application, or a separate web server, or even a CDN (after the index page has been served); there are a few solution patterns out there for this type of thing. Node.js would only be necessarily if the API were written in e.g. Express or Sails or what have you. youtube.com/watch?v=QZVYP3cPcWQ
1

There are several different approaches to accomplish that. I tried about 5 different guides out there, the best I found (and I finally sticked to) was https://thinkster.io/angular-rails - This guide should help you build a basic CRUD app with angular connected to rails.

You use Rails as an JSON RESTful API which responds to Ajax-Requests (Get, Post, Put, Delete). Angular will handle the frontend stuff - sending those Ajax requests to the routes/methods defined in your rails controllers. So yes, of course your AngularJS app can interact with your rails controllers.

This also helped me to understand the setup in the beginning: Instead of the Rails View, you will be using AngularJS as your view:

Angular-Rails Setup

I really love using angular with rails, because setting up the JSON responses (especially with Active Model Serializer Gem) is very easy and quickly done. i deffinitely can recommend it, and I have not encountered any unsolvable problems - so far.

Just go trough this guide I linked and you will see if this setup fits your needs.

Comments

0

The short answer is that your Rails application will have to present some kind of a public API for your AngularJS application to consume. Angular (and it's brethren, like React and Ember) runs client-side, on the browser, and it needs "something" to make AJAX calls against. That "something", i.e. your backend, can be Firebase, Parse, AWS Lambdas, Rails API, etc. Since you already have a Rails application, it probably makes the most sense to add some RESTful API endpoints that use the existing models (and possibly controllers) to consume/produce JSON payloads from/for the client.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.