I was trying to clean up application.html.erb
, by moving parts of the layout into partials. I had the following code for handling flash errors/notifications:
<div id="flash">
<% if flash[:notice] %>
<h3 class="info_box"><%= flash[:notice] %></h3>
<% end %>
<% if flash[:error] %>
<h3 class="error_box"><%= flash[:error] %></h3>
<% end %>
</div>
This code worked fine in application.html.erb
, until I moved it into a file called "_flash.html.erb
" and replaced it with the following:
<%= render 'layouts/flash' %>
In the partial the flash hash was not a recognized object and causes a "You have a nil object when you didn't expect it!" error.
I've move the code back to application.html.erb and all is good. But I couldn't find an answer for accessing the flash hash within a partial. Looking at the Rails Guide for "Rendering and Layouts" I can see that there are various ways for render()
to pass variables into the partial, but I was unsuccessful in figuring it out. Any ideas?