Passing Variable to Partial – undefined local variable or method error

I’ve noticed that if I did not include ‘partial:’ before my partial path, like so:

<%= render 'my_partial', :locals => {:class_Name => "Science", :y => 36} %>

I was required to use the hash+symbol in my partial to access the desired values as others have noted.

<div>
<h1> <%= locals[:class_Name] %> has a y value of <%= locals[:y] %></h1>
</div>

However, including ‘partial:’ before my partial path:

<% render partial: 'my_partial', :locals => {:class_Name => 'Science', :y => 36 } %>

…allowed me to simply call the hash values directly.

<div>
<h1><%= class_Name %> has a y value of <%= y %></h1>
</div>

Just something to keep in mind, this issue tripped me up for awhile when trying to send the locals hash to my partial.

Leave a Comment

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