title: error rendering a flash message with jquery ajax in ruby on rails app
body: <p>Here's the error I get:</p>
  
  <pre><code>ActionView::Template::Error (You have a nil object when you didn't expect it!
  You might have expected an instance of Array.
  </code></pre>
  
  <p>It's referring to this code in a view partial:</p>
  
  <pre><code>&lt;% flash.each do |key, value| %&gt;
    &lt;div class="&lt;%= key %&gt;"&gt;&lt;%= value %&gt;&lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>I set the flash message in my micropost controller:</p>
  
  <pre><code>def create
  @micropost  = current_user.microposts.build(params[:micropost])
  respond_to do |format|
    if @micropost.save
      flash[:success] = "Micropost created!"
      format.html { redirect_to root_path, :notice =&gt; 'success' }
      format.js 
  </code></pre>
  
  <p>Here's my js code in views/microposts/create.js.erb:</p>
  
  <pre><code>$('div.notice').append('&lt;%= render 'shared/flash' %&gt;');
  </code></pre>
  
  <p>And the partial gets rendered dynamically in my application layout:</p>
  
  <pre><code>&lt;body&gt;
    &lt;div class="container"&gt;
      &lt;%= render 'layouts/header' %&gt;
      &lt;section class="round"&gt;
    &lt;div id= "notice"&gt;&lt;/div&gt;
        &lt;%= yield %&gt;
      &lt;/section&gt;
    &lt;%= render 'layouts/footer' %&gt;
    &lt;%= debug(params) if Rails.env.development? %&gt;
    &lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>How do I fix this error? What am I doing wrong?</p>