Want to see the full-length video right now for free?
Rails 5 is on the horizon and getting closer every day! We're currently on beta3, released on February 24th, with a release candidate due any day now. In the mean time, we can take a look at some of the new features coming with Rails 5, and even upgrade using the betas to help the core time search out any bugs in this final stretch.
Rails 5, being a major release, will bring with it a handful of major updates.
Action Cable is the banner feature of Rails 5, brining real time functionality like chat, notifications, and presence to Rails. At its core Action Cable uses Websockets to manage the communication, but with all the niceties you'd expect from a Rails feature.
Initially Action Cable was bringing a whole bunch of new dependencies with it (Faye, Celluloid, Redis), but in the mean time some great work has been done to "adapterify" these dependencies and simplify getting started, removing the dependency on Celluloid, Redis, and even EventMachine. One change added to support that has remained is the switch from Webrick to Puma, but we're fans of this one, having made the switch to puma in Suspenders last year.
Turbolinks is the Rails framework for speeding up page navigation by using a
bit of JavaScript to request the next page in the background, then dynamically
replace the <body>
with the new page content. You can learn more in our
Weekly Iteration episode on Turbolinks. No one's quite sure what happened
to Turbolinks 3 or 4, but here we are on version 5.
In addition to the normal web functionality of Turbolinks, with version 5 it brings support for mobile clients, allowing native mobile apps to interact. Per DHH, "Turbolinks 5 was build for the hybrid sweet spot...".
rails
replaces rake
Starting with Rails 5, all commands will be runnable via bin/rails
! Ok,
this may seem small, but the distinction between bin/rails
and bin/rake
(why do I rake migrate
when I rails generate
???) was troubling for
newcomers, and just one more thing to know. Well no more! Now any command that
was formerly run via rake
can be run via rails
. Also, while we love that
this change was made, rake db:migrate
will still work, just in case that's
stuck in your fingers.
Previously, the rails_api project was created and maintained alongside Rails proper. Its goal was to provide a simplified and customized Rails stack suitable for building APIs. This meant no need to generate HTML, but increased support for JSON.
When the project was started, it was deemed not suitable as part of the core.
But the only constant in life is change, and now the Rails API work has been
merged back into core! Starting with Rails 5, you'll be able to start a new
API project with rails new --api
!
One last headline feature is that Rails 5 will require Ruby 2.2. Thankfully, since Ruby 1.9, upgrades have been pretty straightforward. But, with Ruby 2.2 we get a few niceties like symbol garbage collection. This should help with memory usage of our Rails apps since symbols are used a ton in Rails, but perhaps even more importantly, it means Rails can convert fully to keyword arguments! (check out the Weekly Iteration episode on Ruby keyword arguments for more about them)
There's lots of stuff that doesn't often make the cut as a headlining feature that we're excited about
The Attributes API was a real labor of love for Sean Griffin, cohost on
The Bikeshed podcast, and thankfully it will be included in Rails 5. This
API now makes it possible to declare that a given field on your model is a
custom type (backed by a Ruby class in your application), rather than just the
DB column type. For example, rather than just using a decimal value, you can
declare that the attribute is a :money
type. Neato!
While SQL has had an OR
clause for a while, ActiveRecord has been sorely
lacking. With Rails 5, we can now combine queries using OR
from the comfort
of the ActiveRecord DSL. You can take a look at the commit that adds OR
support for more detail. It works by taking another scope/relation, so as
an example:
Project.completed.or(Project.cancelled)
Similarly, while ActiveRecord has had the joins
method for some time, we
were limited to inner joins. We've already shown you how you can perform
custom joins in the Advanced ActiveRecord trail, but with Rails 5 we'll
now have the ability to perform a left outer join without needing to write the
joins SQL by hand.
Similar to ApplicationController
which is the common base class for all
controllers that you get with new Rails apps, ApplicationRecord
will provide
a base class for your ActiveRecord models. This provides a common place to put
any base model concerns, and hopefully will discourage people from patching
ActiveRecord::Base
. Here's hoping.
With Rails 5, Rails is a bit smarter and more protective when running commands
in the production environment. Now, rake db:reset
in production will require
an explicit override to prevent accidental detraction of production data.
Ok, now that you're fully convinced of all the great stuff coming in Rails 5, there are a few changes that might cause a bit of confusion or require updating your app, so it's good to be aware of them.
Prior to Rails 5, the params
collection was essentially a Hash. With Rails
5, params
are now a custom object. This was mainly done to help prevent
escaping strong-params safety and introducing potential security issues. The
new params object has been designed to be very hash-like in its interface, but
it's possible that you might hit an inconsistency or two if you were relying
on especially hashy behavior of params
.
false
from a callback no longer halts the chainIf you want to halt a callback chain, returning false
will no longer be
sufficient starting in Rails 5. Instead, you'll need to throw :abort
.
This was done to make halting a more explicit action and to prevent accidental
halting caused by unexpected false
return values, but worth keeping an eye
on in your apps if you were relying on this functionality.
While many new things have been added to Rails in version 5, some were removed and put into new gems. Specifically:
assigns
and
assert_template
in testscontent_tag_for
, div_for
view helpersPrior to Rails 5, the HTTP method helpers provided in controller tests (get
,
post
, etc) would use positional arguments for the params and the session:
get :show, { id: 23 }, { visited: false }
Starting in Rails 5, this will move to using keyword args, with a fallback if you have the positional form:
get :show, params: { id: 23 }, session: { visited: false }
before_filter
is deprecated, long live before_action
(and all the other
*_filter
methods)!
The final release of Rails 5 will only be high quality if you try it out and report issues. Yes, YOU. Here's a quick summary of the steps you'll need to take:
bundle outdated
+ bundle update <gem-name>
for each gem listedbundle update rails
github:
for the sourceIf you want to learn more about Rails 5 and are planning to attend RailsConf, then be sure to check out the following: