8

I cannot figure out why my rails views are not recognizing flash[:notice] or flash[:error]. I keep getting the following error regarding the partial view being rendered. The specific error is:

ActionView::Template::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.[]):

In my controller I have

  def index
    @organisms = Organism.all
    flash[:error] = "test"
    flash[:notice] = "test"
    respond_to do |format|
      format.html
      format.json  { render :json => @organisms }
    end
  end

In my index.html.erb file I render out a partial through:

<%= render "shared/flash" %>

The partial has the following code.

<div id="flashes">

  <% if flash[:notice] %>
    <p id="flash_notice" class="messages notice"><%= flash[:notice] %></p>
    <%= javascript_tag "$('#flash_notice').effect('highlight',{},1000);" %>
  <% end %>
  <% if flash[:error] || flash[:errors] %>
    <p id="flash_errors" class="messages errors"><%= flash[:error] || flash[:errors] %></p>
    <%= javascript_tag "$('#flash_errors').effect('highlight',{},1000);" %>
  <% end %>

  <% flash[:error] = flash[:errors] = flash[:notice] = nil %>
</div>

However, if instead of rendering the partial I throw in <%= notice %> it renders out the notice.

If I take the partial code and stick it in the top of the index.html.erb file it renders correctly. Thus, I assume that I am rendering the partial view wrongly?

Any help is much appreciated. Thanks!

4
  • 1
    You're missing the key part: how you're rendering the partial. Also, don't unset the flash at the bottom. Rails already does that for you.
    – Ryan Bigg
    Nov 1, 2011 at 19:57
  • Thanks for the info. I added how I'm rendering out the partial. Nov 1, 2011 at 19:59
  • 1
    u dont need to do this <% flash[:error] = flash[:errors] = flash[:notice] = nil %>
    – Mohit Jain
    Nov 1, 2011 at 20:14
  • Are you doing a flash = nil somewhere? What line is the error pointing to? What is flash[:errors] - you are not setting a value for it anywhere.
    – Zabba
    Nov 1, 2011 at 20:46

2 Answers 2

26

Don't name your partial flash. Ruby on Rails creates a local variable with the same name as the partial. In your case, a flash local variable is being created.

Rename your partial to something other than flash and it should work.

Also, you shouldn't need to set flash to nil at the bottom of your partial. Let Rails take care of that for you.

1
  • 1
    Naming the partial "_flash" was my problem. Thanks Robert for pointing this out. I would have never figured it out. Jan 17, 2012 at 5:15
2

You have to pass the flash to the partial:

<%= render 'shared/flash', flash: flash %>

Or a bit longer:

<%= render partial: 'shared/flash', locals: { flash: flash } %>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.