ruby-on-railsrubyactiondispatch

Undefined method 'slice'


In my Rails 4.2.0 app I have the following code inside a view template:

<% if flash[:tutorial].present? %>
  <% flash.slice(:tutorial) %>
<% end %>

<% flash.each do |name, msg| %>
  <div class="flash <%= name %>">
    <p> 
      <%= msg %>
    </p>
  </div>
<% end %>

It throws an error however:

Undefined method 'slice' for #<ActionDispatch::Flash::FlashHash:0x0818>

Does anyone have an idea what I might be missing or how to get this to work?


Solution

  • flash isn't a Hash. It's an object that behaves (partially) like a Hash. There is no slice method on it.

    It's unclear what you're trying to accomplish with slice here (since you're not outputting the results, and the results wouldn't make sense to output anyways) but if you're trying to achieve what sliceing a Hash would achieve, you can simply create a new Hash:

    <% { tutorial: flash[:tutorial] } %>