Trying to use a Rails flash session, but getting zeros. [] Errors?

<%# Flash-based notifications %>
<% if flash[:error].present? or flash[:notice].present? %>
  <div class="messages <%= flash[:error] ? 'error' : 'notice' %>">
    <ul id="feedback">
      <% if flash[:error].present? %>
        <li><%= flash[:error] %></li>
      <% end %>
      <% if flash[:notice].present? %>
        <li><%= flash[:notice] %></li>
      <% end %>
    </ul>
  </div>
<% end %>

For some reason, as it seems, my attempt to read from a flash drive inside a partial file causes this error, since the flash is set to nil. Do I need to initialize it manually or something else?

This is Rails 3.1.0. The error is on line 2 of the code snippet where it is trying to access flash[:error].

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]

Something is missing for me. I definitely do not cancel it anywhere.

+5
source share
1 answer

They are here - this is what you are trying to access the flash inside the partial. You will need to do the following in your rendering method in order to pass the hash through partial:

render YOUR_PARTIAL, :flash => flash

http://guides.rubyonrails.org/layouts_and_rendering.html#passing-local-variables

EDIT:

: -, _flash.

flash, :

, ( ). : object:

Flash- , .

, , , - :

#_comment.html.erb
<h1>comment.user.name</h1>
<p>comment.body</p>

, - :

render @customer

customer flash, , .

, , .

+10

All Articles