What Is New in Ruby on Rails 5.0
| Category | Highlights |
|---|---|
| New Features | Action Cable for WebSockets, API-only application mode, Active Record attributes API, built-in test runner. |
| Improvements | Bin scripts (rails test, rails restart, rails dev:cache), evented file watcher enabled by default, HSTS headers, STDOUT logging option, Spring config, new README.md generation. |
| Breaking Changes | Removed debugger gem (use byebug), removed deprecated test tasks, removed Rack::ContentLength from default stack, ActionDispatch::ParamsParser removed, ActionController::Parameters no longer inherits from HashWithIndifferentAccess. |
| Deprecations | config.static_cache_control, config.serve_static_files, *_filter callbacks, respond_to/respond_with, redirect_to :back, ActionController::Parameters constants, many ActiveRecord callbacks and methods. |
How does Action Cable enable real-time features in Rails 5?
Action Cable integrates WebSockets directly into the Rails stack, letting you write real-time channels in pure Ruby.
- Server-side channels inherit from
ApplicationCable::Channeland have full access to Active Record models. - Client-side JavaScript uses a built-in consumer that connects to
/cableautomatically. - Scalable deployment works with any Rack-compatible server; you can run multiple workers behind a Redis pub/sub backend.
# app/channels/chat_channel.rb
class ChatChannel < ApplicationCable::Channel
def subscribed
stream_from "chat_#{params[:room]}"
end
def receive(data)
ActionCable.server.broadcast("chat_#{params[:room]}", data)
end
end
What is the API-only mode and how do I generate an API-only Rails app?
Rails 5 can generate a lean stack that omits view-related middleware, helpers, and assets, perfect for JSON APIs.
- Run
rails new my_api --apito create the skeleton. - The generated
ApplicationControllerinherits fromActionController::API, notActionController::Base. - Middleware such as
ActionDispatch::CookiesandRack::MethodOverrideare excluded by default. - Generators skip views, helpers, and assets unless you explicitly add them back.
# config/application.rb
module MyApi
class Application < Rails::Application
config.api_only = true
end
end
How does the new Active Record attributes API change type casting and defaults?
The attributes API lets you declare typed attributes, custom defaults, and even virtual columns without database backing.
- Use
attribute :price_in_cents, :integerto override the default decimal type. - Supply a default value or a proc:
attribute :published_at, :datetime, default: -> { Time.current }. - Define virtual attributes that are serialized/deserialized automatically, e.g.,
attribute :tags, :string, array: true. - Custom types can be created by subclassing
ActiveModel::Type::Valueand implementingcastandserialize.
# app/models/product.rb
class Product < ApplicationRecord
attribute :price_in_cents, :integer
attribute :status, :string, default: "draft"
end
product = Product.new(price_in_cents: "1999")
product.price_in_cents # => 1999
What are the key deprecations and removals that can break existing apps when upgrading to Rails 5?
Several long-standing APIs were removed or deprecated, and they will raise errors if your code still relies on them.
- Debugger support was dropped; replace
debuggerwith thebyebuggem. respond_toandrespond_withwere extracted to the responders gem.- Deprecated
*_filtercallbacks; migrate to*_actionequivalents. - Old
redirect_to :backis gone; useredirect_back fallback_location: root_path. - ActionDispatch::ParamsParser was removed from the middleware stack; configure parsers via
ActionDispatch::Request.parameter_parsers=.
How does the new test runner improve the developer workflow?
The built-in test runner invoked with bin/rails test adds flexible options and better output for Minitest suites.
- Run a single test by appending
:line_number(e.g.,bin/rails test test/models/user_test.rb:42). - Use
-ffor fail-fast,-dto defer output,-bfor full backtraces, and-vfor verbose mode. - Colored output makes failures stand out in the terminal.
- Integration with Minitest options like
-n(run by name) and-s(seed) is seamless.
# Run all tests with verbose output and fail fast
bin/rails test -v -f
Frequently Asked Questions
Does Rails 5 require Ruby 2.2 or higher?
Yes Rails 5 drops support for Ruby versions older than 2.2.
How do I generate an API-only application with Rails 5?
Run rails new my_api --api to create a slimmed-down API app.
Can I still use the old respond_to/respond_with helpers in Rails 5?
No they have been removed and are now provided by the responders gem.
What command runs the new Rails 5 test runner?
Use bin/rails test to execute your test suite.
How do I enable per-form CSRF tokens introduced in Rails 5?
Set config.action_controller.per_form_csrf_tokens = true in your environment configuration.