Ruby on Rails in 2022
It has been about 5 years since I took a look at Ruby on Rails. I have a project at work that will require it, so it is a good time to look at Ruby on Rails in 2022, see what has changed, definitely look into how to smoothly work with macOS BigSur, and check out the IDE scene and which is best for what I need to do.
Ruby on Rails Installation
Thinking back to five years ago, I remember a delightfully simple installation gave me confidence that RoR was going to be a fun and enjoyable experience. Fast forward to 2022 and BigSur; things have changed, but not for the better. macOS BigSur is contaminated with a system level version of Ruby, which is too old to use. Similar to Python we will have to deal with package managers, version managers, etc. just to get a basic configuration installed and functioning.
Starting off, as with all my posts I’ll be working with my standard MacBook Pro configured for software development. At the time of writing I’m working on BigSur 11.6.4. You’ll need to have Homebrew installed so that’s a great place to start if you don’t have Homebrew. Then I would suggest going over to the GoRails site to run through their steps. The steps do work, but follow them very carefully. In particular the step to add rbenv to your .zshrc configuration. I missed that when I initially tried to get all this working.
The instructions will most likely not have the latest version of ruby or rails, its a good idea to check and install the latest release. Or a simple trick for this is to not include a version number on the install command in terminal. After installing ruby and then rails, I restarted the terminal and things seemed to work ok. It was not necessary for me to use the “rbenv rehash” command from terminal.
Configuring Ruby on Rails with a Database and Building an App
I did opt for Postgresql over MySQL as a database. That was pretty straightforward, but don’t forget that the initial user created is your mac username and they do not add a password –> its blank. I don’t like that at all so I did use this command to set a password:
ALTER USER beren PASSWORD 'your_new_password';
Enter that from the Postgresql command line, your database has to be started (brew services start postgresql), and you need to be in the Postgresql command line console (type “psql” in terminal). Once your rails app is generated by entering “rails app myapp -d postgresql” on the command line, you will need to update the database.yaml file with the correct database, username, and password.
With all of that complete, I am able to generate and start a RoR app with all the trimmings: (‘rails new myapp -d postgresql’)! Just reiterating that command for effect 🙂
Start the app from the command line by cd’ing into the project’s root directory and typing “rails server” in terminal (or just “rails s”).
What’s New in Ruby on Rails in 2022?
Things are surprisingly similar. The tooling and command line interface is quite familiar. Deploying an app, checking the logs, etc. is the same. It is comforting to see this kind of stability when other frameworks seems to be in a constant state of flux (Flutter for example or even Symfony).
The recent releases of Ruby 3 and Rails 6 focused heavily on performance and scalability improvements (jit compilers, improved testing services, bulk db uploads, etc.). There also are some improvements in core Ruby and options for better type checking, however some of these have not penetrated into Rails best practices. If you want to hear more, read this.
There are a ton of deprecation’s eliminated in Rails 7, native encryption is supported now. Webpacker, which is robust, but also slow, is replaced as are some other gem’s like Turbolinks. You can get all the details here and here. A lot of incremental improvements, but no “big feature” as you might expect in a major version release. Maybe that is not fair – the support for encryption at the model level simply be tagging an attribute with “encrypts” is pretty cool.
Some things have changed on the command line. In the past you had to enter a command like this:
bundle exec rake test
To run your automated tests. These days things are simpler:
I like that much better. Seems to apply universally that the old rake stuff is now just rails and action. Minor stuff – but my brain appreciates it! Unfortunately – this is not actually universal. Some gems still use the older syntax – sigh…this is true in other “modernization” features where things are kinda half-way between the old way and new way of doing things.
Definitely REST and standardizing on this as a best practice is new. Having a standard for how URLs are constructed is excellent and should make building and maintaining apps much easier. Things like route generation are so much easier as well as how to form links within your app. Very nice indeed!
There are lots of subtle changes to Rails functions, helper methods, etc. so be sure to check SO and the documentation. A great example is in CSS/SASS you used to refer to helper functions to load images with “image-url”, but now this is just “url”. Well 5 years is a long time to be away. Stack Overflow is your friend. 🙂
Ruby on Rails IDEs
I looked at two options for an integrated development environment: JetBrain’s IntelliJ and Microsoft’s Visual Studio Code. Must of this comes down to personal preference. I’ve used both tools for several years at this point. Both tools offer a free version. JetBrains also offers an “Ultimate” version of JetBrains, which I do license, and they also have a decidated Ruby IDE called RubyMine.
Admittedly, I have used JetBrains tools for at least 10 years (maybe more!) for Java, Android, and Kotlin development work. So I’m very familiar with the UI, I know where everything is in terms of configuration, and know how all the debugging tools work. There are plugin’s for Ruby and Rails that give you all the syntax highlighting and intelli-sense code support that one would expect. One issue I noticed is that in .erb files, IntelliJ does not always maintain your indent levels in the HTML. Quite annoying
Microsoft Visual Studio Code
Visual Studio Code is every bit as capable. It is a bit lighter weight, faster to startup, and has an intuitive UI/UX. There are a number of plugins for Ruby and Rails. One thing that can be confusing with VSC is that the intelli-sense support tends to lookup both rails and HTML no matter where you are in the code. When I’m in an HTML tag, I’ll get ruby tags/commands showing up. That shouldn’t happen unless I’m in a <% %> tag.
The killer feature for VSC is not actually VSC, but its Github’s Co-Pilot! Co-Pilot goes beyond intelli-sense to complete block of code completion. You can see this in the image below. Hitting tab will insert the code in grey:
Co-Pilot Labs adds in code translation and code explanation capabilities. See the picture below. The highlighted section of code and the english explanation. Now that is cool! It can translate the selection into a variety of languages like Python and Java. That looked a bit sketchy, but this is a beta so that is to be expected.
It is a bit surprising that neither IDE has a “code cleanup” function for Ruby or Rails like we do for Dart and Flutter (dartfmt). Super awesome feature for Dart/Flutter in my opinion.
Beren’s Ruby on Rails Tricks & Things to Remember
- Getting a favicon working is still something easily forgotten. Drop an icon into app/assets/images and then in the application.html.erb add this line in the <head> block using your favicon image name of course:
<%= favicon_link_tag asset_path('favicon-16x16.png') %>
- The rails console has a funky default configuration. You can fix it up – read here.
- return is optional –> drives me nuts…forgot about that one…I’ll always include a return. It just makes the code more readable and it is only 6 letters. I get the “keep it short” philosophy until it comes to readability.
- Ruby is a bit of a strange language and rails only adds to this. In particular how arrays work and the various functions to manipulate them are super important to understand. Syntax can be a bit odd with how colons are used and the optionality of not completing if else statements…maybe its just me.
- A quick and easy way to not post database passwords to github.com –> after the initial commit, add config/database.yaml to your gitignore file. Not the best solution, but super easy to do quickly while you look for a better solve with environment vars and such.
- Ruby on Rails API
- Ruby on Rails Guides
- Brakeman is the SonarQube of the Rails world.
Conclusions on Ruby on Rails in 2022
You can’t really go wrong with using Ruby on Rails. It is a well established framework with lots of great features to generate boilerplate code, work efficiently with databases, has strong test automation support, and when you run into trouble there is great documentation and lots of help on Stack Overflow.
Regarding IDE choice, my advice is similar. People familiar with Jetbrains IDE products will feel right at home. Same is true with VSC. However, for people starting out new, I do think VSC is a better, more extensible, and lighter weight product. It has a wider variety of plugins and there is no doubt that the Github Co-Pilot, which is not available for Jetbrains products (yet) is a killer advantage for VSC.
The changes for Webpacker’s replacement are quite a bit more that what I originally perceived. Even Sass processing is impacted. I used Dart Sass to re-enable sass processing. https://github.com/rails/dartsass-rails