4

I'm using from controller status:

  flash[:notice] = 'message'
  redirect_to(controller: 'item', action: 'list')

I don't know why the notice don't show up.

I tried and checked many things:

  1. flash.keep
  2. flash.keep[:notice] = 'message'
  3. flash[:notice] works fine with render
  4. redirect_to(controller: 'item', action: 'list', notice: 'message')
  5. flash.now[:notice] = "Hello world"
  6. flash.now['foo'] = "Hello world" with <%= flash['foo'] %> in the view
  7. There is a <%= flash[:notice] %> in the layout

I put the following code to in the layout. flash[:notice] work fine when I the controller method have a view with the same name. The problem happens when I try to reach another controller which don't have a view.

<% if !flash[:notice].blank? %>
    <div class="notice">
        <%= flash[:notice] %>
    </div>
<% end %>

<% if !flash[:alert].blank? %>
    <div class="alert">
        <%= flash[:alert] %>
    </div>
<% end %>   

Can anyone help?

Info:

  • Ruby (2.0.0)
  • Rails (3.2.13)
brunocalado
  • 136
  • 7

3 Answers3

5

Railsguide: http://guides.rubyonrails.org/action_controller_overview.html#the-flash

This should work perfectly fine:

flash[:notice] = "My message"
redirect_to root_url

Or:

redirect_to root_url, notice: "Hello world"

However, it could also be possible you forgot to render notices in your view. Hence, you don't see any notices whatsoever.

For example, something like this should be in your view:

<% if flash[:notice] %>
  <p class="notice"><%= flash[:notice] %></p>
<% end %>
Peter de Ridder
  • 2,379
  • 19
  • 18
  • Thank you for your answer. Is redirect_to(controller: 'item', action: 'list', notice: 'message') equal to: flash[:notice] = 'message' redirect_to(controller: 'item', action: 'list')? – brunocalado Mar 25 '13 at 18:27
  • The two examples I gave are the same, just with a different notation. – Peter de Ridder Mar 25 '13 at 18:43
  • I did the first. Same problem... I'm considering using session for it... Do you think this problem can be because of my rails and ruby version? They are to updated. – brunocalado Mar 25 '13 at 20:20
  • I've heard Rails 3.2.13 is not entirely stable yet, so that could be a problem. Personally I use 3.2.12 with ruby 1.9.3 and I haven't encountered any errors. Anyway, are you sure you actually render the notices in your view? – Peter de Ridder Mar 26 '13 at 07:24
  • Yes, I'm. It even works when I don't go to another controller. – brunocalado Mar 26 '13 at 13:10
0

Try using

flash.now['foo'] = "Hello world"
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
Raghu
  • 2,543
  • 1
  • 19
  • 24
0

I have no idea about what happened.

I updated to ruby 2.0.0 and it just started to work again...

brunocalado
  • 136
  • 7