[en-us] Learning Rails 7 and hotwire
Hey, you!
In this article, you’re not going to learn how to use Hotwire. I’ll just be going to give some of my input about this new approach and explain how I normally set up a rails application from scratch.
First of all, if you want to learn how to use Hotwire, follow this tutorial, which is what I did and by the time you finish you’ll be understanding this new approach and will be ready to apply it by yourself.
My take:
Hotwire is amazing. It took all the good things Rails already did and improved it into a technique that allows you to write little(sometimes none) Javascript.
That’s great for developers that want to write their full stack application but don’t want to have trouble writing or learning React/Typescript/Vue and the whole modern stack frontend developers use these days.
Real time updates with turbo-stream which abstracts all the javascript part from Action Cable and turbo-frames for replacing only parts of pages gives your rails application the look and feel of any other app writen in React or any JS library.
So, give it a chance and try it yourself to build something with it.
Following the tutorial:
I wrote my application based on the tutorial above with some changes that combined serve as the foundation for all my Rails projects.
First, the changes:
I used Rspec as a test framework instead of Mini-test and I wrote a small class to dry-up a the controller responses. Normally I use the responders gem for this, but somehow it’s not working with flash messages and turbo stream response format.
Now, let me walk you through the foundation:
- Docker. I always use docker no matter what. If you get my laptop the only thing installed is a docker desktop application. Nothing else, no database, no programing language, nothing. Even when I need to run one ruby script, I use docker. Follow this tutorial for docker and rails quick start.
- Devise Gem for user management, including login, registration, forget the password, and so on.
- Simple Form gem plays nice with devise so you can build forms with ease. You set up one time and all your forms will be ready to reuse throughout the whole application.
- Rspec gem for tests. Nothing against the default rails test framework, but I feel much more productive with Rspec. Along with it, I also use shoulda-matchers, faker, factory_bot_rails, and rspec-its.
- Rubocop. It helps me write better quality code and once you use it’s hard to go back.
- Dotenv-rails for better management of environment variables.
- Simplecov for checking if I’m covering at least 90% of the project with tests.
One note about tests:
Normally I write tests for all models trying to cover all possible edge cases.
Then I write feature tests using capybara which also acts as integration and end 2 end tests. If I’m writing an API-only application then I favor requests tests. Do not write controller tests. Favor always features or requests over controller tests.
What do you think about Hotwire? Let me know in the comments down below.
See ya!