What Is New in Ruby on Rails 4.1
| Category | Highlights |
|---|---|
| New Features | Spring preloader, config/secrets.yml, Action Pack variants, Mailer previews, Active Record enums, Message verifiers, Module#concerning, enhanced CSRF protection for JS responses |
| Improvements | Automatic test schema maintenance, BACKTRACE env var, MiddlewareStack#unshift, Rails.gem_version helper, render :plain/:html/:body, session#fetch, deep munge opt-out, cookie serializer option |
| Breaking Changes | CSRF protection now blocks GET .js requests, removal of deprecated rake tasks and config.whiny_nils, eliminated Rails.application.railties.engines, updated ActionController constants |
| Deprecations | Removed update:application_controller task, thread-safe config, many Active Record and Action Pack APIs, MultiJSON dependency, several core extensions (e.g., String#encoding_aware?, Time#time_with_datetime_fallback) |
How does Spring improve the Rails development workflow?
Spring keeps a preloaded copy of your application in memory so commands start instantly.
In practice you run the same binstubs you already use, but they now delegate to Spring when it's running:
$ bin/rails console
$ bin/rake test:models
$ bin/spring status
This matters if you run tests or migrations frequently; the typical 2-second boot time drops to sub-second latency. Watch out for stale code after gem changes - restart Spring with bin/spring stop.
How are secrets managed with config/secrets.yml?
Rails 4.1 introduces a dedicated config/secrets.yml file for storing secret_key_base and any other credentials.
Example file:
development:
secret_key_base: 3b7cd727ee24e8444053437c36cc66c3
some_api_key: SOMEKEY
You access them via Rails.application.secrets.some_api_key. This matters if you previously kept API keys in initializers; moving them centralises secret handling and works seamlessly with the encrypted credentials workflow in later Rails versions.
How can I render device-specific templates with Action Pack variants?
Variants let you serve different view files based on a request attribute such as user-agent.
Set the variant in a controller filter:
before_action do
request.variant = :tablet if request.user_agent =~ /iPad/
end
Then respond to the variant just like a format:
respond_to do |format|
format.html do |html|
html.tablet # renders show.html+tablet.erb
html.phone { render :mobile }
end
end
Place the files side-by-side (e.g., show.html.erb, show.html+tablet.erb, show.html+phone.erb). Most teams use this for responsive designs that need separate markup for tablets versus phones.
How do Mailer previews let me see emails without sending them?
Action Mailer previews expose a special route that renders the email body in the browser.
Create a preview class under test/mailers/previews:
class NotifierPreview < ActionMailer::Preview
def welcome
Notifier.welcome(User.first)
end
end
Visit http://localhost:3000/rails/mailers/notifier/welcome to see the rendered email. This matters for design reviews and QA because you can iterate on templates without polluting real inboxes.
How do Active Record enums simplify status fields?
Enums map symbolic names to integer columns, giving you query methods and scopes automatically.
Define an enum in a model:
class Conversation < ActiveRecord::Base
enum status: [:active, :archived]
end
Now you can call conversation.archived!, check conversation.active?, and use Conversation.archived as a scope. The underlying column stores 0 or 1, which is efficient for indexing. This matters when you need a clean, type-safe way to handle state machines without a separate gem.
Frequently Asked Questions
Do I need to modify my existing binstubs to use Spring?
No, new Rails 4.1 apps ship with "springified" binstubs, and you can run bin/rails or bin/rake as before.
Can I keep secrets out of version control?
Yes, you can use environment variables in secrets.yml by referencing <%= ENV['API_KEY'] %> so the file contains no raw credentials.
Will variants break existing HTML responses?
They add new template lookup paths but fall back to the default .html.erb if no variant file exists.
Is the mailer preview route available in production?
By default it is only mounted in the development environment; you can enable it in production by adding the route manually.
What happens if I assign an invalid value to an enum?
Active Record will raise an ArgumentError indicating the value is not a valid enum key.
Do I need to restart Spring after changing a model?
Yes, run bin/spring stop or let Spring detect the change; otherwise you may see stale schema errors.