How does local_assigns work in Rails?

32,225

local_assigns is a Rails view helper method that you can check whether this partial has been provided with local variables or not.

Here you render a partial with some values, the headline and person will become accessible with predefined value.

<%= render "shared/header", { :headline => "Welcome", :person => person } %>

In shared/header view:

Headline: <%= headline %>
First name: <%= person.first_name %>

Here is how you check these variables has passed in or not:

<% if local_assigns.has_key? :headline %>
  Headline: <%= headline %>
<% end %>

Check this document for more detail on the section Passing local variables to sub templates.

Share:
32,225
Lee McAlilly
Author by

Lee McAlilly

Full-stack entrepreneur—I can code, design things, and I have experience starting businesses from scratch. My most recent endeavor—Original Fuzz—has sold over $1MM of guitar straps that I designed, marketed, and manufactured in Nashville, TN.

Updated on April 07, 2020

Comments

  • Lee McAlilly
    Lee McAlilly about 4 years

    I've been googling around about this and can't find the right path. I'm working on a Rails app that is using a method called local_assigns. This appears to be something in Rails or a gem, and not specific to my app, but it's used for rendering a partial in different contexts, such as this:

    <% if local_assigns[:custom_name] %>
      <li><%= custom_name %></li>
    <% else %>
    

    or also this:

    <%= render "discussions/complementary/#{local_assigns[:action] || params[:action]}" %>
    

    Is this is Rails method? Where can I find more documentation about this?

  • sockmonk
    sockmonk over 9 years
    When linking to Rails Guides, it would help to include the Rails version in the URL. That link for Rails 4.2.0 doesn't mention local_assigns that I can tell.
  • mc9
    mc9 over 9 years
    Rather than <% if local_assigns.has_key? :headline %>, why can't we just do <% if headline %>?
  • Tim Diggins
    Tim Diggins about 9 years
    @MikeC this is because if headline is undefined (i.e. not passed to the partial) you'll get an NameError "undefined local variable or method..." raised rather than a falsey value.
  • Eric Anderson
    Eric Anderson over 7 years
    Why cannot you just do if defined? headline then?
  • medik
    medik over 7 years
    @EricAnderson, because raising an error can save your life when debugging :).
  • Joseph N.
    Joseph N. about 6 years
    The guide also states "Testing using defined? headline will not work. This is an implementation restriction."
  • Joshua Pinter
    Joshua Pinter over 5 years
    @EricAnderson I'd like to add that local_assigns is much more explicit and clear when used in a partial. I ran into a confusing situation where I was testing for a local in a partial by the name of format. Using defined?( format ) would always pass because format is a Ruby method for String formatting. It was at this point that we decided to always use local_assigns when testing for the presence of locals in partials. I suggest you and yours adopt the same. It might save you minutes to hours of heartache in the future.
  • Yurii Verbytskyi
    Yurii Verbytskyi over 4 years
    Interesting, but since Rails5 documentation changed from Testing using defined? headline will not work. This is an implementation restriction. to Alternatively, you could also use defined? headline to first check if the variable has been assigned before using it.