Caveat when using Rails' new "strict locals" feature

Updated . Posted . Visible to the public.

In Rails 7.1 it has become possible to annotate partials with the locals they expect Show archive.org snapshot :

# partial _user_name.erb
<%# locals: (user:) %>
<%= user.name %>

# view
<%= render 'user_name' %> <%# this raises an ArgumentError %>

Unfortunately, when some other code in that template raises an ArgumentError (for example an error in the User#name method) you will end up with a confusing stacktrace that looks like you have an error in your render call.

If this happens to you, temporarily remove the annotation to see the correct error.

Niklas opened an issue in rails Show archive.org snapshot , a fix is merged and will arrive in Rails >7.2.2.2 and 8.0.2.

Tobias Kraze
Last edit
Daniel Straßner
License
Source code in this card is licensed under the MIT License.
Posted by Tobias Kraze to makandra dev (2024-06-27 09:55)