Rails 5 was announced by David Heinemeier Hansson(DHH), creator of Ruby on Rails Framework at RailsConf 2015 in Atlanta. Ruby 5.0.0 Beta version was released on 18th December 2015. Apart from the Rails 4, the new features are implemented in 5 version such as Action cable and improved turbolinks that can instantly improve the web development workflow. Here are the Rails 5 Features :
Ruby Version Support :
Rails 5 will only work on Ruby 2.2.1 and above. The following features are the reasons for using ruby 2 or higher :
Symbol Garbage Collector
In Ruby On Rails applications, we usually pass symbols all over the place, doing this opens the possibility of DoS attacks when our memory is consumed by symbols that never get garbage collected. Ruby 2.2.0 introduced changes in its garbage collector to be able to collect symbols.
Allows to do insert a module in-front of the class it was prepended.
Ruby 2.0 introduced first-class support for keyword arguments:
def foo(bar: ‘default’)
foo # => ‘default’
foo(bar: ‘baz’) # => ‘baz’
This will help to reduce memory consumption by Rails applications. Deprecated code removed and Cleanup
#deliver and #deliver! methods have been removed, use new methods deliver_now or deliver_later.
*_path helper in email views has been removed and use *_url.
Protected attributes(attr_accessible) completely unsupported now.
Unsupport for activerecord-deprecated_finders gem.
assert_template and assigns() assertions are deprecated and moved into its own gem rails-controller-testing.
Rails 5 has introduced has_secure_token to include random token in models.
In action view, helper methods such as content_tag_for and div_for were removed from the core and moved out to a separate gem record-tag-helper.
New Turbolinks 3 solves one of the major problems of Rails. Few web apps works really slow cause it reloads the full page from server. This new Turbolinks 3 reloads only the contents of the body, it doesn’t reloads the whole page. It allows you to specify which elements to replace through partials.
Partial replacement feature, is the one of the most significant change in Rails 5. We will be able tell Turbolinks from client side that what content do we need to change/replace and what we don’t. To decide the replacement strategy in DOM, turbolinks will look for HTML5 custom attributes data-turbolinks-permanent and data-turbolinks-temporary.
We can use turbolinks.visit or Turbolinks.replace to update DOM and to trigger a replacement in the client side. Visit will issue a GET to the server to obtain the HTML that must be used to replace our DOM and replace expects from us the HTML that should be used for its operation.
The same functionalities can be triggered from the server-side with redirect_to and render, both redirect_to and render can receive change, keep and flush as options. But redirect_to can also receive turbolinks with true or false to force a redirect.
Action Cable is a framework, used to extend Rails via Websockets to add real-time message passing functionality. This is probably one of the best things in Rails 5. It smoothly integrates WebSockets with the rest of Rails application. Action cable makes it very easy to add realtime features to your app. The reason behind adding this feature was increasing need for such feature. It was introduced to extend Rails functionality with real time messaging utilizing WebSockets.
Action Cable offers a lot of advantages including being an open connection, extremely lightweight compared to other HTTP request. Another advantage is that once a connection made, it will remain open. Through that open line, both server and the client can communicate without reestablishing a new connection. The server and the client can use full-duplex connection of WebSocket to communicate to each other at the same time.
One disadvantage of Action Cable is its inability to cache requests. Another con of WebSocket is the support among browsers as only 70% of the browser market allows for WebSocket connections.
Additional New Features in Rails 5
- #or method in ActiveRecord::Relation
- #belongs_to is required by default
- Active Record Attribute API
- has_secure_token landed in ActiveRecord
- Mysql active record adapter gets Json support
- Render a template outside the controller
- Better Minitest Test Runner
It allows you to generate API – only Rails app and cleans out all the middleware which is not necessary in an app. When you create a new Rails application using new rails API, you will get the configuration which assumes you are working with JSON not with HTML.
Command to Create Rails API Application:
rails new my-app-api –api
Render From Anywhere
All these days we have been using gems like render_anywhere to render views outside of controller. In Rails 5 you can render your views from anywhere. Along with the rendering options, there is also an option available for passing instant variables to templates.
Restart your App with a Rake Command
In Rail 5, you can restart all your apps with the command rake restart. And for migration: rails db:migrate
Note that this list is not conclusive and to know more about all the changes in Rails 5, we recommend you to check out the release announcement on Rails.Org.
- Upgrade to Rails 5
- How To Generate a Swagger Docs For Rails API
- How to generate & add sitemap to your Rails Application
At RailsCarma we use the latest tools and techniques to build your application. Check out our portfolio to understand how we are helping to change the shape of software industry. Get in touch with us now!