Caveat when using Rails' new "strict locals" feature

Posted . Visible to the public.

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

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

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

There is an open issue in rails Show snapshot .

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