← Back to all snippets

Pass local variables to Rails partial in only specific cases

David Anderson | May 23, 2019

You can pass a local variable to a partial in only specific cases by using local_assigns.

# index.html.haml
= render partial: "invoice"
# show.html.haml
= render partial: "invoice", with_account: true
%h5
  = invoice.description
- if local_assigns[:with_account]
  %p
    Account ID: 
    = invoice.account_id
SUBSCRIBE

Drop your email in the box below to subscribe to my newsletter. Once per week you'll get Ruby/Rails tips, guides, job postings, and general thoughts from the web developer trenches.