Icon fonts like Font Awesome are infinitely scalable, look great on high-DPI displays and will give your app a...
RSpec Rails can automatically mix in different behaviors to your tests based on their type tag, for example enabling you to call get and post in specs with the tag...
Rails' default logger prefixes each log entry with timestamp and tags (like request ID). For multi-line entries, only the first line is prefixed which can give you a hard...
...time when grepping logs. Example Rails.logger.info(<<~TEXT) Response from example.com: Status: 200 Body: It works! TEXT With that, the following is written to your log file.
If you are using the routing-filter gem in your Rails 7.1 app for managing URL segments for locales or suffixes, you will notice that the filters do no longer...
...and the necessary parameters are no longer extracted. That is because routing-filter patches Rails' find_routes-method to get the current path and apply its defined filters on it...
When using Rails credentials, you will edit the encrypted credentials for staging or production environments from time to time. To do that you need the secret key which should only...
...to live in :shared_path/config/credentials/:stage.key. If you have a single master.key (e.g. on Rails < 7.2), edit the Capistrano task to find the key at :shared_path/config/master.key instead. Usage
dependent: :restrict_with_error and dependent: :destroy both install before_destroy callbacks, and Rails fires them in declaration order. The restricting associations are declared first, so their checks run...
...works when the restricting association can be declared first. A has_many :through cannot: Rails raises ActiveRecord::HasManyThroughOrderError unless the through association is declared after the association it rides on...
This cards describes an example with a Github Client on how to keep your Rails application more maintainable by extracting domain independent code from the app/models folder to the lib...
...scenarios and not limited to API clients. Example Let's say we have a Rails application that synchronizes its users with the Github API: . └── app └── models ├── user │ ├── github_client.rb │ └── sychronizer.rb └── user.rb...
Rails 7.1 added a new method Rails.env.local?. If you want to stub the Rails env correctly, use ActiveSupport::EnvironmentInquirer like this: # check if the value of stubbed_env is valid...
...allow(Rails).to receive(:env).and_return(ActiveSupport::EnvironmentInquirer.new(stubbed_env.to_s...
You can improve your LIKE / ILIKE search queries in PostgreSQL by adding a GIN index with an operate class ("opclass...
...the Ruby code in your specs. But it can shadow your Project model when Rails resolves an association class by name — and it only does so when the two spec...
...Nothing here changes how app/models/** resolves constants. So you need all three at once: a Rails-managed class defined inside an example group, a group whose description camelizes to a...
Rails has always included a scaffold script that generates a default controller implementation for you. Unfortunately that generated controller is unnecessarily verbose. When we take over Rails projects from other...
...We prefer a different approach. We believe that among all the classes in a Rails project, controllers are some of the hardest to test and understand and should receive special...
I have a form with a dynamic number of fields. Submitting it worked fine until I tried out a very...
When putting phone numbers into web pages, you should use tel: links so smartphone users can click those numbers to...
Getting CSS (and JS) live reloading to work in a esbuild / Rails project is a bit of a hassle, but the following seems to work decently well. We assume that...
...you already use a standard "esbuild in Rails" setup, and have an esbuild watcher running that picks up your source code in app/assets and compiles to public/assets; if not change...
After an upgrade to rails 7 I noticed that async reindexing jobs of Searchkick were failing for Model.reindex(mode: :async, wait: true): /home/a_user/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/searchkick-5.3.1/lib/searchkick/relation_indexer.rb:142:in `block in batch_job': undefined...
...setups. This can become pretty messy. The trick: spin up throwaway controllers with rspec-rails' anonymous controller helper, drive real requests, and classify the response. Simplest possible case
...SomeController isn't sufficient here. It isolates routes. A bare routes.draw mutates the global Rails.application.routes. The helper swaps in a fresh RouteSet and restores it afterwards. It names the subclass...
We are using assignable_values for managing enum values in Rails. Nevertheless Rails is adding more support for enum attributes, allowing to have a closer look at the current feature...
...to our still preferred option assignable_values. Active Record enum attribute interface By default Rails is mapping enum attributes to integers: class Conversation < ActiveRecord::Base enum :status, [ :active, :archived ]
To return non-HTML responses (like XLS spreadsheets), we usually use the respond_to do |format| format.xls do # send spreadsheet...
When deleting a record in your Rails app, Carrierwave automatically takes care of removing all associated files. However, the file's container directory will not be removed automatically. If you...
...together with its child records, like a movie with its showtimes. This lesson covers Rails' nested attributes and how to build and test such forms. Important Work on this lesson...
...records in a single transaction — all of it or nothing. You can explain how Rails form helpers name nested inputs and how those names turn into the nested structure of...
Ever needed to use a global variable in Rails? Ugh, that's the worst. If you need global state, you've probably reached for Thread.current. When you're using Thread.current...
...chapters 2 "Meaningful Names", 3 "Functions", 4 "Comments" and 17 "Smells and Heuristics" 📘 Growing Rails Applications in Practice — chapters 5 "Dealing with fat models" and 7 "Extracting service objects"
📄 Best practices for writing code comments Video ▶️ All the Little Things — Sandi Metz, RailsConf 2014, 40 min: refactoring a nested conditional into small objects, live Discuss with your mentor...
...can specify your preferred version like so: bundle _2.1.2_ update --bundler Older Ruby and Rails cannot use the latest bundler 2 version, so you need to stay on bundler...
...term, you can use PostgreSQL’s trigram similarity search. Writing a fuzzy query in Rails User.where("similarity(name, ?) > 0.3", "John") This finds all users where the name is similar to...