Intergrating Angular JS with rails - ruby-on-rails

I need some guidance to figure out how to incorporate Angular inside rails.
Reason for choosing Rails : I like their opionated approach to do things right. Also migrations, gems are really cool.
Reason for angular : I was researching and looking for framework best suited for SPA. Backbone seem too abstract. I had choice to make between Angular & Ember. I started reading Angular first and it made sense to me. So i never went to read about ember.
Reason for Angular & Rails: I researched and tried using small scale framework like grape, slim ( Yes i use php too ). But i feel need to stick to rails for long term scope of project. And personally i like doing things rails way.
So here is where i need help,
I have rails project in Rails 4.
Sign-in , sign-up everything is created as followed in Michael Hartl tutorial. Have tweaked stuff based on my requirement.
So post-sign-in or post-sign-up steps a view from show action of users controller is rendered.
I figured i'll need different layout files so i created same for outer pages and inner pages, respectively.
I don't know how to proceed i want to make use of the angular templates and routes for my single page app ( which resides post sign-in ). I am flexible if there is another way. I just need a guide how to use angular seemlessly with rails making use of rails controller to handle my rest request and using routing provided by angular to navigate around in SPA.
Hope i am clear. Feel free to edit this.

here is a great railscasts from ryan bates: http://railscasts.com/episodes/405-angularjs
also here is the source for that railscasts you can get ideas from there:
https://github.com/railscasts/405-angularjs

I'm not familiar with SPA, but I have been working on a tutorial for integrating rails and angularjs, which I'm refining over time. It may provide some answers here - in particular I am using the angular routing to provide a single-page app as you describe: http://technpol.wordpress.com/2013/09/03/angularjs-and-rails-tutorial-index/

I would suggest you to not mix angular into your rails app. Keep both of them separate.
So you could either place the whole of your angular app in the public folder of your rails app or keep it completely away from the rails app. This is more like a service oriented architecture, where your rails app serves as a back end serving as an api and the front end(Angular app) consuming that api.
There are many many nice articles the covers how to handle the authentication/authorization
in angularjs with REST APIs.Authentication with AngularJS and a Node.js REST api
Coming to the rails side for building REST API, Grape is a nice choice. Here is a nice series explaining some best practices about grape.
HTH!

I'd suggest taking a look at this tutorial: How to Wire Up Ruby on Rails and AngularJS as a Single-Page Application. I have used it for a few personal projects, so I am sure that it is up to date as of late 2014. If you want to view it in action you can head to http://goodmatches.herokuapp.com, and you can view the repo.

Try half-pipe gem which makes using bower for managing javascripts assets much easier.

Related

Can AngularJS architecture interfere with a framework like Ruby on Rails at some point?

I have a web application that has most of it's logic on ruby on rails, and lately I've been getting good at AngularJS but I'm not a master at it. What I like about angular is that it lets you be very declarative on your javascript code knowing if an element is being referenced on the html unlike jquery. Also it has nice filtering options but that would not be needed cause rails handles that on the server-side. Is AngularJS meant to work as a stand alone framework or it can coexist with server-side frameworks?.
This is more of a discussion question, so not an ideal SO question.
Angular and Rails can happily coexist.
You can use the asset pipeline and load Angular, in the same way that you would any other JS library.
There is a good tutorial here, also, this Railscast is quite handy although it is a bit out of date now.
My advice is to have a crack at it, and then come back to SO and post specific bugs or challenges you are hitting.

What would be the best way to use AngularJS with Ruby on Rails?

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.
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.
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:
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.
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.

Ruby on Rails separate front & back

I've been using Ruby on Rails since a little more than one year now and I've always do it in a casual way, I mean, everything in one place (front & back), using the standard .html.erb file populated by the associated controller method.
Otherwise, today in our project, I have the need to separate the front and the back end for multiples reasons (code maintainability / clarity, better architecture, more reactivity, etc...).
I've done plenty of researches, watch some conferences (1, 2, 3), but didn't find my solution yet. It looks like to be a question that comes often, but what is the best practice/tools to separate the backend and the frontend of a Ruby on Rails app?
I don't feel we need (yet) a huge JS framework like React/EmberJS/Angular/etc...
First I was thinking about something like Middleman/Jekyll and make the communication via JSON and API calls, but it seems like that it's not a good solution for dynamic website.
So is there a frontend framework that works well with a Rails API and which is easily maintainable and upgradable (add feature/extension to it like gems)?
Thanks for your insights.
A friend of mine wrote this great article/tutorial on Rails as a backend API.
http://blog.launchacademy.com/the-basics-of-building-an-api-in-ruby-on-rails/
As well as this tutorial on Rails/Ember.js
https://github.com/diegodesouza/Project-Planner-EmberJS
You can get an idea of how it's done and implement your preferred front end framework.
Hope it sheds some light on this question.
I have a similar setup as one of the commenters on the question.
I'm using Rails mainly for just the project structure, to define some page layouts, and for ActiveRecord.
I then have my JSON APIs defined using the Grape API framework.
I have a SPA, written on AngularJS that lives in the public/ folder. It doubles as my mobile app, made possible by phonegap. If my Angular app didn't double as my mobile app, I could've possibly just used the asset pipeline to serve up the SPA. To compensate for that, I have a separate build task written in Grunt to minify/uglify my JS/CSS assets before I deploy them out to production.
I also use Comfortable Mexican Sofa for my static content pages.
It took some trial and error to get things right, but overall I find that this setup serves me pretty well.

Rails: Single page website, How to organise controllers/views etc

I have a single page website using ruby on rails as the framework.
On the single page I have a
header
section 1
section 2
section 3 etc
footer
The problem I am having is this, how do I organise the layouts/views/controllers.
Only in one section I have a form which needs processing and is linked to a database table (model). Can anyone advise me on how I can organise my ROR application?
The question on "how to make a single-page application in Rails" has been asked numerous times on SO like here, here and here.
You can try to look for Single Page Application (SPA) tutorials and there is a video on Vimeo here where Prateek Mohan Dayal shows off how to make a single-page application in Rails and Backbone.js.
On Back-End of an SPA
On the backend side you're most likely going to arrange your controllers and models the same way as usual with Rails, but you'll be retrieving JSON-data as views (i.e. AJAX stuff) as they're the easiest way to handle data client-side.
So think about what models you have and how you're going to work with them in controllers an arrange the code as usual.
As to how to do things AJAXy with sending JSON data to and from Rails you can have a look at Yehuda Katz Rails 3 book in this SO answer or you could have a look at this simple example that uses Rabl to develop a JSON API.
On Front-End of an SPA
It seems that most Rails developers are using either Backbone.js or Ember.js for single page application development. Both of them have some powerful javascript based view-model and databinding concepts which helps out developing the web app.
Another way to get started is to look at Addi Osmani's TodoMVC project that is a single page application (i.e. a web based todo-list) developed in many different javascript MVC frameworks. Both frameworks mentioned above are available for your perusal:
TodoMVC Backbone version - source code
TodoMVC Ember version - source code
Here are some screencasts:
Peepcode also has some starter projects and a video tutorial on Backbone.JS though like with all it's screencast's it costs.
Code school also has some tutorials for Backbone.JS where the introduction level is free.
Well, if you don't create a new layout, everything is going to be in the application layout (app/views/layous/application.html.erb), in this file you're going to create you html for the header and the foother. Also in this file you'll see a <%= yield %>, this yield is going to show you view of your controller.
Sorry about my english, I hope this help you. Also you could check the Rails Tutorial
is very helpful and very descriptive. Also you could check the RailsCast videos.
For the section 1 and section 2 you could check the rails guide to undertand how to show something in any numbers of yields. Here is the link Understanding yield

Looking for a fully functional Rails application using Backbone.js

Backbone.js website has some examples. But barring the first one others are not open source. I am looking for a fully functional (meaning it just works) Rails application to study. The app does not need to have too many functionalities. I looked at github and all the apps are broken in some ways.
Recently i found https://github.com/malclocke/fulcrum and it seems to be the best Rails/Backbone example but its not mentioned on the backbone website. Its also a very functional pivotal tracker clone.
I have been working on some non open source projects that use a Rails and Backbone.js stack. Both frameworks can be integrated fairly easily. Of course, it depends on how the application is setup and how you configure each framework to control more or less business logic.
To get both frameworks to play with each other:
Make Backbone collections and models for each Rails model
Route resources for each Rails model
Setup the URL property for the Backbone collections and models to work with your rails routes
Use fetch() and save() in Backbone to get and post data with Rails
I wrote a german language noun trainer using RAILS and backbone.js. It was done a long while ago while I was still learning but you can peek at it if you want.
https://github.com/bradphelan/ohmyderdiedas
I've been actively working on Myelin: http://sourceforge.net/projects/myelin/ (funded from a corporate source)
There are some caveats:
This is essentially a first for me with every technology in there... from rails, to backbone / jquery / rspec... you name it... it's new, so take the code with some grains of salt ;)
I didn't use the Backbone routing, and built a very simple 'router' of my own.
You'll need ganglia and rrdtool installed (macports if you're on a mac should work)
You'll need to alter the development config for sure.
The models, are (mostly) straight up backbone, and I use sync often in the controllers so those should be pretty good examples.
The views are a little more chaotic.
If anyone needs help with anything, just drop a line to me on sourceforge.
There's now a gem in development that provides generators, called rails-backbone. It's Open Source and getting better every day. As of today it's up to date with current Rails 3.1 (actually 3.2 now), esp. including Asset Pipeline, which is very relevant to backbone.js.

Resources