Last week, during RailsConf 2017, Rails 5.1 shipped.
form_tag, and replacing it with
form_with. I can’t wait to try it.
But the reason I love Rails isn’t the big new features. It’s the little, constant improvements. It’s those quality-of-life changes that make me happier when I’m writing Rails apps. And Rails 5.1 is full of them.
More consistent tag helpers
Have you used Rails’ tag helpers, like
Use calls like
tag.br, and you can stop worrying about parameter order and juggling two different methods:
These new tag helpers support HTML5 by default, and even let you create your own elements:
Assert more than just differences
assert_difference, I spent way too much time juggling local variables in tests:
assert_difference, it’s much clearer what you’re trying to do:
assert_difference only checks changes in count. But
assert_changes can check non-numerical changes, like changes between two strings, or between nil and something else:
Instead of a string, you can give it a lambda:
to: can be anything that compares with ===. That’s nice when you know something about the value, but don’t know what it is, specifically:
In some Rails code, you’ll see the
delegate method used. Delegation is helpful when you want to add behavior on top of another class, without inheriting from it:
But sometimes you want to forward everything to the class you’re wrapping.
There are a few ways to do this with Ruby, using
SimpleDelegator. But to better match the
delegate_missing_to was added to Rails 5.1. It does exactly what it says:
Now, any call to a method that’s not in the Player class will search on
alias_method_chain is gone!
And as of Rails 5.1,
alias_method_chain is now officially gone – replaced with prepend.
New versions of Rails are always exciting. But it’s the details that give Rails its beauty. The small changes that make you happier with the code you write every day.
How do you find those changes? Dive into the changelogs. Take a look at interesting pull requests. See which of the new, small, 5.1 features will make your life that little bit easier.
And when you find some cool stuff, don’t keep it to yourself. Share it here, so we can all learn something new!