Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a regular Rails app and a Rails API?

In the process of learning Rails, I read about how we could combine it with some front-end MV* JavaScript frameworks — such as Backbone.js, Angular.js, or Ember.js — to improve the UX.

This introduced (to me) the concept of using Rails as an API, instead of a web app.

So, now, I am pretty confused: what is the difference between a regular Rails app and a Rails API?

like image 580
Thibaud Clement Avatar asked Jun 28 '15 16:06

Thibaud Clement


3 Answers

According to the official rails website, there are three main differences between a rails web application and a rails api:

1 - The api app is configured to start with a more limited set of middlewares than normal. Specifically, it will not include any middleware primarily useful for browser applications (like cookies support) by default

2 - In the api app, ApplicationController inherits from ActionController::API instead of ActionController::Base. As with middlewares, this will leave out any Action Controller modules that provide functionalities primarily used by browser applications.

3 - The api app is configures the generators to skip generating views, helpers and assets when you generate a new resource.

You can always convert your rails app from either one of these to the other one. To do so, follow the steps in the reference I mentioned above.

like image 153
Meysam Feghhi Avatar answered Sep 27 '22 20:09

Meysam Feghhi


A regular Rails app will use the rails views (erb or haml) to render pages directly. That is to say, it will process the data AND render this data in views, answering directly the client request with a HTML page.

A Rails API will just process your action, and assume someone else is doing the job of rendering the view for the client. Therefore, a Rails API is expected to return data in an appropriate format, like JSON, XML, or just a JS piece of code to execute. It is then the job of frontend frameworks like AngularJS to receive, parse, and do something with the data (like update some HTML, etc.)

In a nutshell,

  • Classic Rails application are all-in-one applications, where the processing and rendering are both handled by Rails. The apps may lack or responsiveness however, as full pages are rendered, but it's usually much faster to code this way.
  • Rails API are just serving intermediate results. It will focus on just delivering the data. This can be better if you have strong requirements for the design/responsiveness, as you are more flexible with the frontend libraries you can use. Usually only the data is transferred, so in addition it can be faster. There are some problems with APIs. For example with one-page apps + full AJAX, it may be harder to set up a forward/back behavior on the user browser. Also, using APIs will require more work, but if you have many devs, you can agree on the interfaces, and parallelize the work server/frontend.

Now, it's not a black or white answer I'm giving. You can totally have a Rails app mainly built as a Web app, but with some API actions that give more responsiveness to some pages. An exemple of this, is to have an autocomplete form, that is pulling the data via AJAX calls.

like image 42
Cyril Duchon-Doris Avatar answered Sep 27 '22 18:09

Cyril Duchon-Doris


I found a pretty clear answer in Yoni Weisbrod's Rails API Mini Guide:

The fundamental difference between an API and a regular Rails app is that an API returns data for further processing, rather than data that is meant to be viewed directly. Therefore, rather than producing an HTML document (with CSS and/or Javascript) that looks pretty, APIs produce simple information structures that can be further processed by whatever will be consuming our API.

like image 33
Thibaud Clement Avatar answered Sep 27 '22 19:09

Thibaud Clement