Rails: Make template locals available as a hash to the template itself

Created on 17 Feb 2015  路  3Comments  路  Source: rails/rails

This is a common piece of code:

# Template 1
<%= render 'some/partial', magic: variable %>

# Template 2
<%= render 'some/partial' %>

# some/partial
<%= defined?(magic) ? magic : '' %>

That defined? check is pretty gross. I'd much rather be able to do:

# some/partial
<%= locals[:magic] %>
actionview

Most helpful comment

These fucking Rails people have thought of everything! local_assigns is exactly this and already available. TIL!

All 3 comments

These fucking Rails people have thought of everything! local_assigns is exactly this and already available. TIL!

This is actually an kind of private API as it's a local variable eval'ed here. There's an isssue (#15700) to document it. Probably the naming local_assigns will remain

:facepunch: :horse: brohoof

Was this page helpful?
0 / 5 - 0 ratings