Should we use Rails routes or React routers - ruby-on-rails

I am new with React and today I was working on react-routers. I am not clear which routes to use. My choices are Rails routing or React router. What are the difference between these?

React should be used for rendering and Rails should be used for the API.
Each react app should have its own server-rendered path, with calls to a rails API on the backend. The API should definitely be routed with rails, but only pass back the data you need to the react app. Rails 5 has ApiController you can use for your controller superclass.
TL;DR - React is frontend, rails API on the backend.
Also, you should really determine if you need something so heavyweight on the backend. Some lighter ruby alternatives are Sinatra and Padrino.

You can use them for both server side and client side. If you are fully MEAN stack then you can use that. If you are in Rails stack then it's not possible.

Related

Rails Frontend, Backend, and API

Does your Rails app have to be API-Only to be able to use the API generator? I want to create my fronted and backend using Ruby on Rails. Then build a mobile using the API.
Does your Rails app have to be API-Only to be able to use the API
generator?
No, it can be a normal web application and you can build an API alongside it. The term API-only came from Rails 5 where it introduced a way to generate a Rails API-only applications that are different from normal web applications. For more information, have a look into these guides
You can access your API throught a different route, as in www.myapp.com/api/request, and access your Rails application throught www.myapp.com/request, for example

How to get Rails and all requirements running to use React?

Iam a novice in rails and trying to write my first React frontend. I have read enough to know that there are different methods to use React with rails. I decided to split frontend and backend, and use rails in API mode. I cannot find good sources explaining what I need on my server to get rails and react going, and why. I read so far about npm, node.js, Heroku and so on, but i lack the knowledge to decide what to pick and i dont understand the interactions yet.
Does someone know a good tutorial, some sources or keywords to point me to a direction?
If you want to use Rails just in API mode with React on frontend then you can separate those apps completely.
I.e. use Rails/Puma server for API and have a separate server (Nginx/Apache/Node.js) to serve the frontend React app and other static assets.
For using react as your client-side application and rails for the server-side you need two different applications. As Rails is MVC I suggest you use just ruby and Sinatra server.
In this situation be careful about the cross-origin errors you have to enable it while using two agents.
Take a look at this.

Ruby on Rails to Ionic

I have a working Ruby on Rails application that I would like to bundle with Ionic.
How do I show my RoR pages in the Ionic app? Do I have to treat it as an API? What happens to the RoR styling?
I have read http://www.dovetaildigital.io/blog/2015/8/21/rails-and-ionic-make-love-part-one but am unsure how to make this work for my application.
Thank you!
The recommended way to access dynamic information from Rails application is to use API. The trick is to use RESTful routes to handle HTTP calls when you develop your RoR application. Check out this article. Of course you can point ionic (basically phonegap) to load externally hosted mobile friendly RoR application if that fulfills your need reasonably.
Your best bet for rendering data from the Rails backend in an Ionic app is to access the data as JSON.
See the documentation on the Angular HTTP module here.
https://docs.angularjs.org/api/ng/service/$http
Another option would be to return HTML, but you'll lose a lot of benefits on Angular presenting it this way.
Thanks,
Dan

Using your Grape API from within Rails

Is it a good idea to use already implemented Grape API endpoints from within your Rails Application controllers?
I'm making an application where my Rails Controllers have shared functionality with my API. I've already implemented the API of an application, now I'm implementing the controllers.
So my question is, is there a way to issue a request from a controller to my Grape API endpoint without it going through the internet? If so, is it a good idea or not?
Do not call Grape endpoints from your Rails controllers instead make Ajax calls to your Grape endpoints and consume the API from the web/mobile front end. It is a good idea to have a single point of entry to your application rather than exposing Grape routes as well as Rails routes.

How to use Rails as DDP server with Meteor.js client

We have a Rails app that acts HTTP API only. On the client side, Ember.js is currently used. We are not overly impressed by Ember and really like the approach Meteor.js takes. So we'd like to exchange the client side with Meteor.js and communicate with the Rails server via websockets that speak the Data Distribution Protocol (DDP), so we can keep using the models, mailers and controllers in Rails. Implementing server side of DDP should be easy.
However, we're unsure how to make Rails talk websockets. We found Reel, which seems to make it easy to accept websocket requests in a standalone environment. Reel seems great as we'd like to implement the DDP on top of the Celluloid stack anyway. But what about running Reel in the Rails environment? Would we need "rails runner" for that? And we'd like to keep using the existing controllers to dispatch incoming requests (like, to add/change/remove resources). Is that even possible without having the request coming through Rack?
Any input is appreciated.
It's a bit late, but I've implemented DDP in Ruby, you can check it out here:
https://github.com/d-snp/ruby-ddp-server
It includes an implementation of EJSON as well. It's built on top of celluloid-websocket, and can be ran simply as a rack app.
I've made an integration with RethinkDB that can be used as a reference to build your own collections implementation.
https://github.com/d-snp/ruby-ddp-server-rethinkdb
I've also made a sample chat application that can be found here:
https://github.com/d-snp/celluloid-rethinkdb-chat
It's something that I have been longing to do as well, to integrate old "legacy" Rails code. Here is the best way I have found:
Since you would not be using any of Rails router/controller/views, but just the ability to read data and push it to the client, I recommend you use Rails to create JSON apis to the database, and deploy the code, then in Meteor you can consume the data via the http package, this would happen on the server at a regular interval and populate the MongoDB with the normalized data you need, then it would serve the browser client.
I am working on such an application that will keep a normalized version of the data in Mongo, and a relational version of the data in mySql (through Rails) this way I can preserve the legacy Rails functionality that I dont want to rewrite in JS, and get the benefit of Meteor for the one page that I need it most.

Resources