-1

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?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Tintin81
  • 9,821
  • 20
  • 85
  • 178
  • 2
    What are you trying to do? I'm not sure why you'd want to use `slice` this way. – user229044 Mar 27 '17 at 16:48
  • If I have a flash like `{ :tutorial => "Message", :notice => "Message", :success => "Message" }` I would like to delete all keys except the `:tutorial` key. – Tintin81 Mar 27 '17 at 16:54
  • And do what with the result? Once you have a hash containing only the key `tutorial` and the corresponding value, what would you do with it in the view? – user229044 Mar 27 '17 at 16:59
  • Render it to the user :-) The tutorial is so paramount in my app that it should override / replace all the other types of flash messages. – Tintin81 Mar 27 '17 at 17:01
  • 2
    But what does that have to do with `slice`? Why not just render `flash[:tutorial]`? This is the information I'm trying to tease out of you here, nothing about `slice` is necessary to render whatever value is in `flash[:tutorial]`. – user229044 Mar 27 '17 at 17:02
  • OK, but I might have values in `flash[:notice]` and `flash[:success]`. What to do with them if I don't want to display them? – Tintin81 Mar 27 '17 at 17:05
  • ... You don't have to do anything with them, `flash[:tutorial]` already only returns the value associated with the key `:tutorial`. I'm still completely unsure about what you think the problem is here. – user229044 Mar 27 '17 at 17:10
  • 1
    Maybe you can assign the flash value to a variable as hash and modify as: `flash_hash = flash.to_hash` and then `<%= flash_hash.each { |k,_v| flash_hash.delete(k) if k != 'notice' } %>` – Sebastián Palma Mar 27 '17 at 17:10
  • @meager: Thanks for your help. I added more code to my post above. I simply want to get rid of all keys except `:tutorial`. Because there might be several keys. And I don't want to throw all of them at the user. – Tintin81 Mar 27 '17 at 17:14
  • 1
    Ok, so given you know the *only* key you want to pay any attention to is `tutorial`, why do this loop? Why not just output `<% if flash[:tutorial] %>

    <%= flash[:tutorial] %>

    <% end %>`?
    – user229044 Mar 27 '17 at 17:17
  • Because I am dealing with lots of controllers and lots of flash messages here. Some controller actions have tutorial flash messages, others don't. But when there is one, it should have precedence. – Tintin81 Mar 27 '17 at 17:20

1 Answers1

6

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] } %>
user229044
  • 232,980
  • 40
  • 330
  • 338