Ruby on Rails API and web services - ruby-on-rails

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

Related

Rendering data from WordPress REST API in Rails

I have a WordPress site and a Rails site hosted on separate servers. I need to pull information from some custom post types on the WordPress site and display them on the Rails app. At a minimum, I would like to display all the post titles (hyper-linked) – there's no writing to WordPress needed.
I'm not well-versed in Ruby on Rails or HAML – I'm basically just a WordPress/PHP developer – so I'm at a loss as to how to begin. I'm thinking the best way is to pull data from WordPress using the WP REST API (http://v2.wp-api.org) plugin, but I don't know how to go about rendering data from an API in RoR/HAML. If you could point me in the right direction, that would be awesome!
Yep exposing an API on the WordPress side would be a good option, you're on the right lines.
In this situation you could do is start by creating a controller, route and view (hopefully some tests too) then within the controller you can request the data from the WordPress API. For this you will need a HTTP client, Net::HTTP is part of the Ruby stdlib or alternatively something like faraday which offers a little more flexibility. From here you can pass the response data into an object that you can use in your HAML views as required. You don't want to be assigning the JSON to an instance variable in your controller and accessing it as a Hash in the view, create a class that parses the response and provides some methods to allow you to access the data using method calls.
This would get you going in terms of a basic implementation but once you get the basics working it may be worth doing a little refactoring. For example what you would have now is a small wrapper around the API and this could be a logic extraction from your controller. The advantage being that the controller code becomes easier to understand and also should you wish to use the wrapper somewhere else within your application that becomes much simpler.
Edit: You could also use a REST ORM like her

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.

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.

Intergrating Angular JS with 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.

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

Resources