Ruby on Rails - Migrating from Rails to Sails: Comparison and Implementation Guide- ruby on rails tutorial - rails guides - rails tutorial - ruby rails
Why are you migrating from Rails to Sails?
- Ruby is an good programming language and easy to learn.
- As a web developer with Ruby background, there are chances that you write JavaScript for a better user-facing application.
- You are a smart Ruby developer (you maybe like the DRY principle) and that’s why you make use of Rails MVC framework to build your Ruby apps.
- Your life has been going so simple and happy until you started feeling left behind because there is a new tool that everybody is using and talking about - Node.
- From research, you have come to realize that Node is just JavaScript on the server and because of the performance features it comes with, it has gained massive popularity.
- Composed, Node from Rails features is good.
- Sails is a Node framework inspired by Rails, therefore, with your little JavaScript skills, we will build an application in Node using Sails while putting in practice everything we know about the Rails architecture.
- This article is structured in such a way that Rails and Sails’ core concepts are discussed side by side starting from their background , down to their REST implementation.
- We have a long way to go so grab a cup of coffee and relax so we can have a better time learning.
Rails
Ruby programming language is used to written the Rails and it is created by David Hansson.
- Rails were open sourced by David in 2004.
- The framework was designed using the MVC (Model-View-Controller) pattern for better separation of concerns.
- This particularly made Rails intuitive to use because as long as a developer is comfortable with Object-Oriented Programming and the MVC pattern, one can easily jump on Rails with basic understanding of Ruby concepts and start building awesome web products.
Sails
- Sails was written in JavaScript, precisely Node.js. Sails framework is created by Mike McNeil and was inspired by Rails.
- This fact that Sails was inspired by Rails made them have a lot in common with each other - the architecture (MVC), directory structure, assets management, etc.
- For these details, if you are coming from Rails and have basic JavaScript skills, then you are good to start writing Node apps with Sails.
Version Managers & Installation
- Version managers are useful when you have to build different kinds of projects on the same machine and the same platform but require different versions of the platform.
- For instance, you could have v.2.3 of Ruby on your machine but you are working with a team on a given project that strictly requires v1.9.
- The possible solutions to this are virtual machines (which, of course, are harsh on machine resources) or version managers.
- Keeping in mind that we are going to build two projects side by side on different platforms (Ruby & Node), let’s install their version managers:
Ruby (Rails)
- Ruby is known as Ruby Version Manager (RVM)
- To get RVM on our machine, we want to first install the author’s public key:
- Then we can go fast to install RVM using curl:
- We can run any RVM command to confirm installation:
- With RVM installed, we can have multiple installations of Ruby versions but let’s just install one as default:
- The install command is used to install specific versions and the default flag sets the particular version as default.
- To confirm installation by running:
Node (Sails)
- Node is known as Node Version Manager (NVM). We can install NVM with the following curl script:
- We want to add NVM to your path using:
- We need to use the latest version of Node so to install, run:
- Nowadays we have installed both Ruby and Node, our platforms are ready.
Package Managers
- Code re-use is beautiful but it’s more enjoyed when you re-use other’s. These days, the community of a given technology is calculated with the amount of contributions made by the community in mind.
- When building large projects, there is a tendency that you are going to rely on other people’s solution to enhance yours.
- The way these solutions are managed and kept in sync are through package managers.
Ruby (Rails)
- Rails package manager is called gem and you can install any gem that is useful to you using the following syntax:
Eg:
- Where sass is the name of the package we want to install.
- Rails itself is a gem so we install it straight away:
Node (Sails)
- Node’s package manager is npm and comes installed when we install Node. npm has much in common with gem:
Eg:
- Sails, just like Rails, is also a Node package and can be installed with:
- The -g flag tells npm to install Sails globally so it can only be installed once and used for subsequent projects.
- If the -g flag is ignored, Sails can be installed in the directory the command was run, which could be inside a folder labeled node modules.
Setup
- We have gone through the preparation process for a Rails and Sails project and now it’s time to set it up.
- Just a reminder, we will build the same project twice. One with Rails and the other with Sails.
- Because we are coming from a Rails background, we will first handle a task in Rails and then see how we can complete that in Sails.
- Create a folder named cm-rails-sails where cm stands for code mentor. It is inside this directory that both projects will live.
Rails
- To create a new project in Rails, inside the created folder, run:
Sails
- Just as we have done for Rails, in the same cm-rails-sails folder, run:
Command Line Tool
- Project scaffold and boilerplates can really be a pain in the skull because it is a repetitive process that just bores the brain to perform.
- Both frameworks provide command line tools for scaffolding projects (just as we have seen above) and generating a project’s pieces like controllers, models, etc.
- Listing all the commands for both frameworks are irrelevant because trust me, you will never memorize them once.
- The best way to learn them is to encounter them through the learning process.
Directory Structure
Let’s have a look at the directory structure for both projects and see what we can make of it:
Rails
Sails
- Sails looks simpler which would make it friendlier to approach.
- Rails is more matured because it has been around for a while and way older than Sails
Database Configuration
- We will leave the database configuration as defaults which will leave us with sqlite for Rails and disk for Sails:
Rails
Sails
ruby on rails tutorial tags - ruby , rail , ruby on rails , rail forum , ruby on rails tutorial , ruby tutorial , rails guides , rails tutorial , learn ruby
Models and Migrations
- Let’s have a look at the data layer of MVC - M. Both frameworks use the same approach to generate models, but the difference is what happens under the hood:
Rails
Sails
- As you can see the only difference is rails and sails. Both commands generate the following models:
Rails
Sails
- Sails do not require migration files to define its schema.
- The schema of a given model is defined right inside the model’s definition.
- Each model file must export an attributesobject. The attributes object defines the schema of a given model.
Rails require that you migrate by running:
- On the other hand, migration is not required for Sails.
- Waterline ORM which is what Sails uses does the magic for you when you start hitting the database with requests.
Routing
- Routing is a key aspect of every web application, so both Rails and Sails take routing matters to heart.
- create some routes on both projects:
Rails
- We have created four routes to be handled by Todo controllers which we are yet to create.
- Now let’s see how we can do the same in Sails:
Sails
- Sails uses an object’s key-value pairs to map the controller’s action methods to its respective routes.
- The first word in the keys describes the HTTP verb that will be used to take care of this request.
ruby on rails tutorial tags - ruby , rail , ruby on rails , rail forum , ruby on rails tutorial , ruby tutorial , rails guides , rails tutorial , learn ruby
Controllers
- We have our data layer in place and our routes are defined.
- Next thing to do is to use controllers to handle requests coming from routes.
- In doing so, we can either send data as response or receive data as request with the help of our models:
Rails
- We have created three action methods to handle our routes: index, new, and create.
- The first two handles get requests so Rails can demand a presentation layer-the view. We will tackle that soon.
- The last handles form submission so no view is required, rather, we just redirect to the home page.
Sails
- The three methods implemented on Rails are now available in our Sails project.
- The index method fetches all todos and renders a view with the todo.
- The new method just renders a form in a file named new.
- The create method creates a new todo and redirects to the home page if the attempt was successful.
Views
- From the previous section which we discussed controllers, it is easy to conclude that we just need two views in the todo app-a view to show all todos, and another to show a form to create a todo.
- At this point, if you try to run with rails server or sails lift, you have run into errors that try to tell you that the view template files are nowhere to be found. Let’s create them:
Rails
Learn ruby - ruby tutorial - learn ruby tutorial - sail todos view in ruby sails migration - ruby example - ruby examples - ruby programs
The same thing we saw in Rails applies to Sails. As a matter of fact, the template engine for Sails, ejs was inspired by the template engine of Rails, erb.
REST (BONUS)
- RESTful services have become the best way to go when building solutions that will be consumed by different platforms (PCs, Mobiles, IoT, etc).
- Before we wrap up our discussions, let’s quickly have a look on how to generate resource endpoints for REST and how to return JSON data rather than rendering views:
Rails
- Add the following route in Rails route config:
- When we do so, we are served with a collection of routes that promotes building REST apps:
- We can confirm this by running:
- If we architected our application in such manner, we would have to return JSON rather than views in our controller action methods’ responses:
Sails
- The case is quite different in Sails. Sails uses automatic route binding for RESTful services. Let’s see for ourselves. Run:
- The above command generates a
- model (Todo.js) and a
- controller (TodoController.js) for
- our projects.
- But that is not all. Once we
- create an action method in the
- controller, Sails automatically
- binds the method name to a route
- named after the action.
- For instance, if we create an
- action method named new,
- Sails will create a route
- todo/new and bind it to
- new.
- Just like what we saw in Rails,
- we can now start returning JSON
- rather than rendering views: