Rails-api and rendering html pages - ruby-on-rails

I am using rails-api gem for an api project....
I have two or three webpages how do I do it SO that html will make an ajax call and consume JSONS
Where can I keep the html pages and render it..

Rails::API is a subset of a normal Rails application, created for
applications that don't require all functionality that a complete
Rails application provides. It is a bit more lightweight, and
consequently a bit faster than a normal Rails application. The main
example for its usage is in API applications only, where you usually
don't need the entire Rails middleware stack nor template generation.
You can start adding middleware and subframeworks back to Rails-api - but then your turning your lean and mean racecar into a minivan.
A better solution may be to create a separate rails app which serves the HTML pages and has the full rails package.

If they're static pages (with static urls), you can put them in /public. It's generally considered bad practice, but it will work.

Related

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

Best practice for multi-page application using Rails and Angular?

There are tons of tutorials out there for creating single page applications using Rails and Angular. They usually pass all the routing to Angular and leave Rails for the activerecord stuff. I find this frustrating because there are times when I just want a plain HTML page with minimal client side bells and whistles.
What is the best practice for a multi-page application, where I can use both Rails routes and Angular UI-router?
I would use the Angular router so all of your routes are in one place. The best part about angular is you can add as much or as little as you want. So you can have static pages handled by the angular router.

Ruby on Rails API and web services

I am fairly new to web application development and I have a pretty noob question. As I understand, it is possible to write the backend of a web application - models, business logic, etc. using Rails and write the backend - the view - using just HTML, CSS Javascript (without using Rail's form helpers, embedded ruby, etc.) and that the said view would render data exposing a Rails API and making AJAX requests to it. Everytime I googled "rails ajax requests API" I have only found tutorials that explain how to use AJAX w/ embedded ruby instead of tutorials that clearly separate how to make a Rails API and make AJAX requests to render data. So my questions are:
1) If any of the above statements are incorrect, could you let me know?
2) Can someone point me to tutorials that help me understand the theory and implementation of the aforementioned topics?
Thanks!
The REST API generated by Rails can be consume by any client, if you want to use pure HTML and CSS withouth the .erb files (for me brings many advantages when developing) you can access to the Rails API data by AJAX.
I let you the following tutorial:
http://blog.project-sierra.de/archives/1788
Hope it helps

Using AngularJS in Ruby on Rails app

I have a existing project in Ruby on Rails.
What is the best way to use AngularJS in Ruby on Rails app?
I want to use AngularJs for only specified modules, not for create SPA. This is good way?
What I have seen colleagues do in order to achieve this sort of integration with an existing rails app is:
Include angular.js and relevant dependencies in the specific app pages that are to be 'angularized'
Interpolate whatever data is needed to bootstrap the angular controller into the html template which contains the angular app. This might include data about the resource being operated on. I've seen this done by rendering a RABL template inside of a haml/erb template.
Using that interpolated data, call whatever API methods you need to get additional data on the fly. This is usually just a matter of implementing json handlers for routes you've already created.
I can't say whether this is best practice, but its an easy way to get started fast.
Best of luck, angular is a very enjoyable tool to work with once you get used to it.

AngularJS & Rails - Seperated apps/deployments or one app?

Most of the resources/example about rails and angular on the internet just put them together. AngularJS goes inside of rails under app/assets. This feels reeeaaaly dirty to me. Is it a good idea? What if we decide at some time we won't be using rails and we move to, I don't know, sinatra? How hard would it be to port?
What would be pros/cons of everthing in a single rails app and what would be pros/cons for two seperated apps?
Thank you!
Even when placing Angular (or any other client-side MV* framework) inside your Rails app, you are pretty much keeping the separation of concerns intact. That is, you have a Rails API serving JSON (or similar) data, and a separate Javascript framework using that data to render appropriate views. If you ever wanted to use a different server-side API, you can do so, and still utilize your entire Javascript directory as is.
Placing the client side framework in Rails is simply a matter of convenience. It comes with an organized directory structure and the ability to serve your HTML, which is essentially all you're using it for when it comes to your views. But again, these views aren't really tied to Rails, as they're just HTML and Javascript, so you can easily move them to a different platform when necessary.

Resources