How does local_assigns work in Rails?

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 rails 3.2 Passing local variables to sub templates.

Updated rails 7 link: ActionView/Base.html

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)