This Week in Rails

November 20, 2021

Automated shard swapping middleware, standardised error reporting interface and more!

Hey, this is Greg, bringing you the latest news about Ruby on Rails.

Support <form> elements without [action]
By default, when a form is declared without an action attribute, browsers will encode the form's fields into the current URL. Prior to this commit, none of the form construction variations supported declaring a form without an action attribute, form_with, form_for, and form_tag all default to url_for({}) when a url or action option is omitted, but with this change, when they are set to false, the form will be rendered without an action attribute.

Support authenticity_token option in button_to helper
This PR adds support for passing authenticity_token option to button_to, the same way as in form_with and form_for calls.

Introduce field_name view helper
The field_name helper and corresponding FormBuilder#field_name method provides an Action View compliant way of overriding an element's name attribute. For instance you can do the following:
text_field_tag :post, :tag, name: field_name(:post, :tag, multiple: true)
  # => <input type="text" name="post[tag][]">

Automatic shard swapping middleware
This PR adds a middleware that can be used for automatic shard swapping. The design is similar to the database selector middleware in that the resolver is provided by the application to determine which shard to
switch to. The selector also takes options (the only supported one is lock yet) to change the default behavior of the middleware.

Standardised error reporting interface
Rails.error is a new error reporting interface, with two block based methods. handle, which swallows errors and forwards them to the subscribers:
Rails.error.handle do
  1 + '1' # raises TypeError
end
1 + 1 # This will be executed

and record, which forwards the errors to the subscribes but lets it continue rewinding the call stack:
Rails.error.record do
  1 + '1' # raises TypeError
end
1 + 1 # This won't be executed.

For cases where the blocked based API isn't suitable, the lower level report method can be used:
Rails.error.report(error, handled: true / false)

Filter attributes in SQL logs with debug
SQL queries are logged when the Rails log level is set to :debug and previously, filter attributes were not masked in the logs in this case. With this change, filter attributes will be masked as [FILTERED] in the logs, but he filtration is applied only when prepared_statement is enabled.

Speed up collection rendering and add support for multifetch collection handling in jbuilder
This PR speeds up collection rendering by taking advantage of the existing collection renderer in Action View, and it adds support for multifetch collection handling to make it more efficient.

33 people contributed to Rails since last time. We couldn't cover all the changes, but you can see all of them here. Until next time!

About This Week in Rails

Your weekly inside scoop of interesting commits, pull requests and more from Rails.