7

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.

ZazOufUmI
  • 3,212
  • 6
  • 37
  • 67
  • 2
    For the project I currently work on, the API-like back-end is based on Grape (REST-like API micro-framework for Ruby) which complements Rails very well and the front is in Angular. The communication is done via API calls and JSON responses. Although I'm not quite sure about the efficiency of Angular as front, I'd would suggest you have a look at Grape as a good decision for the back-end thanks to its readability and maintainability. – Brozorec Oct 10 '15 at 08:25
  • Another option would be using rails-api which is a stripped version of rails as an alternative to grape. – Manuel van Rijn Oct 10 '15 at 11:25

2 Answers2

1

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.

Diego
  • 594
  • 2
  • 10
  • 29
0

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.

Jon Worek
  • 191
  • 13