In Rails 7.2 the new default for config.action_dispatch.show_exceptions is rescuable. :rescuable: It will show a Rails error page in the response only for rescuable exceptions as defined by ActionDispatch...

stack trace and a good debugging experience. :all: It will show a Rails error page in the response for all exceptions (previously true) :none: It will raise for...

api.rubyonrails.org

Rails offers a way to prepend (or append) view paths for the current request. This way, you can make the application use different view templates for just that request.

...action :prepare_views def index # ... end private def prepare_views if prepend_view_path Rails.root.join('app', 'views', 'special') end end end If is true, Rails will first look into app/views/special...

Development environment setup Rails Composer Basically a comprehensive Rails Template. Prepares your development environment and lets you select web server, template engine, unit and integration testing frameworks and more.

...in minutes using an application template. With all the options you want! Code generators Rails Bricks A command line wizard. Once you get it running, it creates sleek applications.

Rails wraps your parameters into an interface called StrongParameters. In most cases, your form submits your data in a nested structure which goes hand in hand with the strong parameters...

...expect/permit/require when passing them to e.g. ActiveRecord. Note that expect is only available from Rails 8. There are cases where it might be fine to read directly, like: # Okay

TL;DR: Rails ships two methods to convert strings to constants, constantize and safe_constantize. Neither is safe for untrusted user input. Before you call either method you must validate...

...w[User Post Test]) if class_name class_name.safe_constantize.new # either User, Post or Test else Rails.logger.error "This should not happen!" end Handling unresolvable constants The safe_constantize method is defined in...

Rails default config uses the ActiveSupport::Cache::NullStore and disables controller caching for all environments except production: config.action_controller.perform_caching = false config.cache_store = :null_store If you want to test caching...

path end end RSpec.configure do |config| config.include CachingHelpers end 3. Now stub the Rails cache and clear it before each example: RSpec.describe SomeClass let(:file_cache) { ActiveSupport::Cache.lookup_store...

Add gem 'database_cleaner' to your Gemfile. Then: Cucumber & Rails 3+ # features/support/database_cleaner.rb DatabaseCleaner.clean_with(:deletion) # clean once, now DatabaseCleaner.strategy = :transaction Cucumber::Rails::Database.javascript_strategy = :deletion Cucumber & Rails 2

...available cucumber-rails for Rails 2 automatically uses database_cleaner when cucumber/rails/active_record is required -- but only if transactional fixtures are off. To have database_cleaner work correctly: Add the attached...

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...

class World < ApplicationRecord; end class World::Earth < ApplicationRecord; end Without any further configuration, Rails will produce the following database table names. >> World.table_name => "worlds" >> World::Earth.table_name => "world_earths...

This card will teach you how to index, search and rank your Rails models in a PostgreSQL full-text index. We will do this without using any gems...

Why Rails has multiple schema formats When you run migrations, Rails will write your current database schema into db/schema.rb. This file allows to reset the database schema without running migrations...

...by running rails db:schema:load. The schema.rb DSL can serialize most common schema properties like tables, columns or indexes. It cannot serialize more advanced database features, like views, procedures...

Rails offers the fresh_when method to automatically compute an ETag from the given record, array of records or scope of records: class UsersController < ApplicationController def show @user = User.find(params...

Rails is split into a large number of (sub-) frameworks. The most important and central of those are activesupport (extends the Ruby standard library) activerecord / activemodel (ORM for Rails)

...gem 'actionmailer', require: false) as well. When you look into the definition of rails/all (in railites-x.x.x/lib-/rails/all.rb) you can find the exact path for requiring the sub-frameworks.

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' 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.

...older formatter API. Maybe there will be a fix for that eventually. Update cucumber-rails to >= 1.6.0: bundle update cucumber-rails Upgrade cucumber: bundle update cucumber Make sure you have...

If you're suffering from a huge de.yml or similiar file, cry no more. Rails lets you freely organize your dictionary files in config/locales. My organization works like this: config/locales/rails.de.yml...

...modified Rails boilerplate config/locales/faker.de.yml modified Faker boilerplate config/locales/models.de.yml model names, attribute names, assignable_value labels config/locales/views.de.yml text in the GUI config/locales/blocks.de.yml if you organize your CSS with BEM you can...

By activating strict_loading you force developers to address n+1 queries by preloading all associations used in the index...

Rails 7.1 added the normalizes method which can be used to normalize user input. It lets you define the fields you want to normalize and how to normalize them. In...

...validators or models (app/normalizers) you can use the following initializer. # config/initializers/normalizers.rb module Normalizers; end Rails.autoloaders.main.push_dir("#{Rails.root}/app/normalizers", namespace: Normalizers...

...Basics and Previews Chapter "Task H1: Sending Confirmation Emails" from Agile Web Development with Rails (in our library) Ensure that the receiving e-mail is valid Using the Truemail gem...

Ensure that development and staging are not sending out e-mails by accident Rails: How to write custom email interceptors Check if you have a local mail server listening...

...a cookie's "secure" flag do? Is it still relevant with HSTS? Look at Rails' API for managing cookies How do you set and delete cookies? What are signed cookies...

What are encrypted cookies and how do they work? Learn about Rails sessions (which are not the same as 'session cookies') Learn about the SameSite cookie attribute...

#pluck is commonly used as a performant way to retain single database values from an ActiveRecord::Relation Book.pluck(:title, :price...

If you're using a Redis cache in Rails (e.g. :redis_cache_store), it's possible to configure additional parameters for your Redis connection. Example config for Rails 7.2

...too low value for timeouts can make your cache timeout too often. Note that Rails explicitly sets connect_timeout, read_timeout and write_timeout, which means that only configuring timeout...

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 ]