Note that there are some explanatory texts on larger screens.

plurals
  1. PORailstutorial: Putting flash messages in partial yields error "undefined method `each' for nil:NilClass"?
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/3111844/flash-messages-in-partials-rails-3">Flash Messages in Partials (Rails 3)</a> </p> </blockquote> <p>I am doing Michael Hartl's Railstutorial and <a href="http://ruby.railstutorial.org/chapters/sign-up#code-layout_flash" rel="nofollow noreferrer">listing 7.26</a> adds flash messages to the application layout:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; . . . &lt;body&gt; &lt;%= render 'layouts/header' %&gt; &lt;div class="container"&gt; &lt;% flash.each do |key, value| %&gt; &lt;div class="alert alert-&lt;%= key %&gt;"&gt;&lt;%= value %&gt;&lt;/div&gt; &lt;% end %&gt; &lt;%= yield %&gt; &lt;%= render 'layouts/footer' %&gt; &lt;%= debug(params) if Rails.env.development? %&gt; &lt;/div&gt; . . . &lt;/body&gt; &lt;/html&gt; </code></pre> <p>This works fine.</p> <p>However, I tried to clean up this code by creating a _flash.html.erb in my partial folder...</p> <pre><code>&lt;% flash.each do |key,value| %&gt; &lt;%= content_tag(:div, value, class: "alert alert-#{key}") %&gt; &lt;!-- &lt;div class="alert alert-&lt;%= key %&gt;"&gt;&lt;%= value %&gt;&lt;/div&gt; --&gt; &lt;% end %&gt; </code></pre> <p>...and than using...</p> <pre><code>&lt;%= render 'partials/flash' %&gt; </code></pre> <p>...in my application layout all my Rspec tests start failing with the following message for each test:</p> <pre><code> Failure/Error: before { visit signup_path } ActionView::Template::Error: undefined method `each' for nil:NilClass </code></pre> <p>The key issue seems to be that flash is nil because wrapping my _flash partial in an if statement like this...</p> <pre><code>&lt;% unless flash.empty? %&gt; &lt;% flash.each do |key,value| %&gt; &lt;%= content_tag(:div, value, class: "alert alert-#{key}") %&gt; &lt;!-- &lt;div class="alert alert-&lt;%= key %&gt;"&gt;&lt;%= value %&gt;&lt;/div&gt; --&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>... yields the same error message about NilClass as above and wrapping it in an if statement like this...</p> <pre><code>&lt;% if flash %&gt; &lt;% flash.each do |key,value| %&gt; &lt;%= content_tag(:div, value, class: "alert alert-#{key}") %&gt; &lt;!-- &lt;div class="alert alert-&lt;%= key %&gt;"&gt;&lt;%= value %&gt;&lt;/div&gt; --&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>... breaks the flash messages from working (because 'if flash' is always false).</p> <p>I have two related questions:</p> <ol> <li><p>Why/how exactly does using the partials/flash solution change the behavior of a rails app?</p></li> <li><p>How to change my partials/flash so it will work?</p></li> </ol> <p>Thanks! </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload