brunocalado
brunocalado

Reputation: 136

flash[:notice] doesn't work with redirect_to

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:

Upvotes: 4

Views: 1720

Answers (3)

Raghu
Raghu

Reputation: 2563

Try using

flash.now['foo'] = "Hello world"

Upvotes: 0

brunocalado
brunocalado

Reputation: 136

I have no idea about what happened.

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

Upvotes: 0

Peter de Ridder
Peter de Ridder

Reputation: 2399

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 %>

Upvotes: 5

Related Questions