![rail5](https://uploads.sitepoint.com/wp-content/uploads/2015/06/1433587885rail5-231x300.png)
Rails 5 is right around the corner (currently targeting Fall 2015) and there are some exciting features coming up. If you are running a Rails shop, you need to prepare your apps for this major release.
Don’t worry. As always, we at Sitepoint will guide you throughout the migration process when the release date approaches. For now, let’s catch you up on what’s coming and how it will improve your development process.
Major Improvements
There are some amazing announcements that will fundamentally shift how we work with Rails. New features like Action Cable and improved Turbolinks that can instantly improve our web development workflow. Let’s look at each of them in detail.
Merging Rails API
Over the last few years, many thanks to Backbone.js and Angular.JS, the number of Single Page Applications (SPAs) are on the rise. The last few projects that I have built have been purely SPA and using Rails for these cases was an overkill. I tried dabbling with Sinatra and even pure Rack applications, but ended up writing too much boilerplatee. The routing was lacking compared to Rails and there were too many security vulnerabilities that I had to handle. I had to either write it on top of the community driven rails-api gems or live with traditional Rails.
In Rails 5, the rails-api gem will be merged into core allowing the use of Rails as a simple JSON API. Personally, I find this a great addition. I won’t have to look further while building an API for my javascript (and other) appliciation clients.
Ruby 2.2.1
Ruby 2.2 was a great addition to the Ruby community. Not only did it bring a huge performance boost to Ruby, but it also introduced a slew of new functions. In addition, garbage collection of symbols was widely celebrated. Legend has it that Dragons of Valeria rained down rose petals on the Seventh Kingdom when this announcement was made. OK! Maybe I took that too far, but you get the general picture.
NOTE: Before migrating to Rails 5 you need to make sure that your app works on Ruby 2.2.1.
Due to these enhancements, Ruby 2.2+ was a ripe candidate for Rails 5. Rails 5 will only work on Ruby 2.2.1 and above.
Turbolinks 3
Traditional web apps tend to be slow due to full page reloads. One way to solve this is just reloading the bare minimum content area. Turbolinks solve this by just reloading the contents of the body from the server, instead of refreshing the entire page. While this improves the performance to a certain extent, this is also relatively slow. Turbolinks 3 aims to solve this.
Tubolinks 3 allows you to retain most of your page and selectively update certain regions through partials. This is very similar to how SPA’s work, and you can choose to do all this from the server. Sounds great, right?
This is a great feature as the flock moves towards single page apps. However, at first glance, it looks like you’ll have to manage the partials manually. This means that you’ll have to remember which part of the app to reload at a specific point. To me, it adds a lot of clutter to my code and makes it easy to screw up. Personally, I prefer using something like React, which handles this very well with the Virtual DOM.
Note: The way React works is, it maintains a Virtual DOM where all the manipulations happen. It then diff’s the Virtual DOM and the actual DOM and makes only the required changes. It ensures that DOM is hit only when absolutely required and the changes are bare minimum.
Having said that, for the folks who don’t like to meddle around with JS, this offers an instantaneous performance boost.
Action Cable
Many projects these days use WebSockets to push live updates to the client. While most client browsers have started to support this, we still need a robust client on the server to manage the subscribers and send an update signal appropriately. This feature is available out of the box in some of the newer frameworks, like Phoenix for Erlang. However, the Rails community had to resort to third party implementations, like Pushr, to get this working.
Note: For the uninitiated, WebSockets is a W3C standard that opens up a duplex connection from your browser. Servers can talk (i.e., push updates) to clients as needed, and asynchronously update the client’s state without a full page reload. For instance, this is how Gmail loads new emails without reloading the entire page.
Rails traditionally offers all the tools that one needs to build a great app out of the box. That’s one reason why it is so popular, especially among the startup community. The lack of WebSocket support was a reason for major discontent among the community. It looks like the Rails core team took note of this and came up with Action Cable. We’ll have to wait for some time to see how this is going to work in real life, but it’s exciting nevertheless.
Some Niceties
Rake Inside Rails
For many a Rails noob, having to figure out when to use rake
and when to use rails
is a source of confusion. Now you don’t need to switch context between the rake
and rails
commands. You can run all Rake tasks with the rails
keyword. For instance,
rake db:migrate
will now become:
rails db:migrate
This may not look like much on the surface, but this will make the lives of beginners much simpler. Rails 5 will also add a restart
command that quickly restarts the application.
Active Record Changes
One change that almost went unnoticed was the update to belongs_to
. When you create a Student belongs_to
Class relation, it was possible to create a student without an associated Class relation. This lead to a lot of data inconsistencies. With Rails 5, he parent has become mandatory. If you try to insert an empty record here, Active Record will throw up.
Changes to Controller Tests
If you’re testing what your template renders in your controller, you are doing it wrong. A simple change to your template will leave you with sleepless nights, hunting down the culprit. That’s why, in Rails 5, assert_template
is deprecated. However, you can continue using assert_select
to check if a specific DOM element is present.
If you’ve been testing the instance variables inside a controller method, you should note that assigns
is also going away.
If you’re writing controller tests, the new decree is that you should be concerned with cookies, HTTP return code, and DB calls, if any. Wait, that sounds just like an integration test. Well, you got me there…controller tests will likely be dropped in favor of integration tests in the future.
Note:* Checkout Rails Dom testing for more details and best practices.
Wrapping Up
With javascript playing a dominant role in web development today, web frameworks are getting reduced to mere API services. The Rails core team realizes this and is moving in the right direction. Feel free to join us in the comments section for a discussion.
Frequently Asked Questions about Rails 5
What are the major changes in Rails 5 compared to Rails 4?
Rails 5 introduced several significant changes compared to Rails 4. The most notable ones include Action Cable, a new framework that integrates WebSockets with the rest of your Rails application. It allows for real-time features to be written in Ruby in the same style and form as the rest of your Rails application. Another major change is the introduction of API mode, which allows you to create a slimmed-down Rails application, ideal for API-only applications. Rails 5 also introduced Turbolinks 5, which allows you to create Single Page like Applications directly from Rails stack, without having to resort to a heavier SPA framework.
What is Action Cable in Rails 5?
Action Cable is a new feature in Rails 5 that integrates WebSockets with the rest of your Rails application. It allows for real-time features to be written in Ruby in the same style and form as the rest of your Rails application. This means you can write real-time applications with Rails, introducing a level of interactivity and responsiveness that was previously difficult to achieve.
How does API mode work in Rails 5?
API mode in Rails 5 is a way to create a slimmed-down Rails application, ideal for API-only applications. When you create a new Rails application using API mode, it starts with a smaller set of middleware than typical. It also omits any middleware for handling cookies or sessions, making it a leaner and more efficient framework for API-only applications.
What is Turbolinks 5 in Rails 5?
Turbolinks 5 is a feature in Rails 5 that allows you to create Single Page like Applications directly from Rails stack, without having to resort to a heavier SPA framework. It makes navigating your web application faster by using JavaScript to replace the page content when you follow a link, without needing to reload the entire page.
How does Rails 5 improve test runner?
Rails 5 introduced a new test runner that enhances the testing experience. It includes features like colorized output, the ability to test specific lines of code, and smart test re-running. It also provides options to stop running tests after the first failure, making it easier to debug and fix issues.
What is the purpose of ApplicationRecord in Rails 5?
ApplicationRecord is a new superclass for all models in Rails 5. It’s an abstract class that sits above all of your app’s models, similar to how ApplicationController works for controllers. This allows you to specify default behavior for all models in one place.
How does Rails 5 handle JavaScript dependencies?
Rails 5 introduced a new way to handle JavaScript dependencies with the new Rails Command Line Interface (CLI). The new CLI includes commands for adding JavaScript dependencies directly into your Rails application, making it easier to manage and update your JavaScript libraries.
What is the purpose of the ‘rails’ command in Rails 5?
The ‘rails’ command in Rails 5 is a new command line interface that replaces the old ‘rake’ command. It provides a unified command line interface for all tasks, including running tests, starting a server, and creating new resources.
How does Rails 5 improve support for PostgreSQL?
Rails 5 introduced several improvements for PostgreSQL. It added support for the ‘belongs_to’ association required by default, materialized views, and the ability to add foreign keys in parallel, among other features. These enhancements make it easier to work with PostgreSQL in Rails applications.
What are the system requirements for Rails 5?
Rails 5 requires Ruby 2.2.2 or newer. It also requires a working installation of SQLite3, unless you’re using a different database. For development, you’ll need a Unix-like operating system such as Linux or macOS. Windows is also supported, but you may need to install additional software.
Hola! I'm a Fullstack developer and a strong advocate of Mobile first design. I'm running a digital children's startup for kids and I lead the engineering efforts there. In my free time I ramble about technology, and consult startups.