0

flash[:notice] gets erased as soon as I use redirect_to in rails 2.2.2 It's a super simple ruby application, so it shouldn't be to hard to find what's going on (although, I couldn't find it obviously...)

My routes.rb looks like this

resources :happenings
root to: "happenings#index"
post "/happenings/save"

And my HappeningsController looks like this

def index
   @happenings = Happening.all
end

def save
   flash[:notice] = "saved"
   redirect_to root_path
end

Now if I'm debugging and I add puts flash[:notice] to the save action, it will print "saved" to my terminal if I add `puts flash[:notice] to the index action, it will print an empty line to the terminal. So it obviously has been erased by the time the index action gets executed.

Any ideas? Thanks a lot?

Edit, Here's a bit more information. The save action get's called by a form

<form method="post" action="/happenings/save">

The following is the server's output when I submit a form (and request the save action)

Started POST "/happenings/save" for 127.0.0.1 at 2015-07-02 23:16:31 +0200
Processing by HappeningsController#save as HTML
  Parameters: {"happening_name"=>"Test happening", "happening_time"=>"2015-07-24T13:15"}
Can't verify CSRF token authenticity
Redirected to http://localhost:3000/
Completed 302 Found in 1ms (ActiveRecord: 0.0ms)


Started GET "/" for 127.0.0.1 at 2015-07-02 23:16:32 +0200
Processing by HappeningsController#index as HTML
  Rendered happenings/index.html.erb within layouts/application (0.1ms)
Completed 200 OK in 32ms (Views: 28.4ms | ActiveRecord: 0.0ms)
akmozo
  • 9,829
  • 3
  • 28
  • 44
sjbuysse
  • 3,872
  • 7
  • 25
  • 37
  • Can you make sure you aren't reseting any cookie from application controller? Because flash messages are stored in cookie. You can also try flash.now[:notice] = '....' – Rubyrider Jul 03 '15 at 14:23
  • @sjbuysse I think that you need the [`flash-message`](http://stackoverflow.com/questions/tagged/flash-message) instead of [`flash`](http://stackoverflow.com/questions/tagged/flash) tag, I edited it. – akmozo Jul 04 '15 at 12:58

3 Answers3

1

https://stackoverflow.com/a/12340941/2011580 If you are having problem to keep the flash after redirecting to another path, then use this.

flash.keep

in your method, before redirecting.

Community
  • 1
  • 1
James B. Byrne
  • 1,048
  • 12
  • 27
0

You might want to do something like this:

def save
  redirect_to root_path
  flash[:notice] = "saved"
end

And then place

<%= flash[:notice] %>

anywhere in your index.html.erb.

Felman
  • 11
  • 1
  • 1
    That doens't work, just tried it. Also, I'm wondering if it even makes sense, since you're redirecting to the `root_path` (and leaving the `save` action) before you set the `:notice` – sjbuysse Jul 03 '15 at 07:51
  • That will never work. you are getting redirected before setting the flash message. – Rubyrider Jul 03 '15 at 14:22
0

Can you make sure you aren't reseting any cookie from application controller? Because flash messages are stored in cookie. You can also try

flash.now[:notice] = '....'

before redirection you can use:

flash.keep(:notice)

source: http://guides.rubyonrails.org/action_controller_overview.html

A tip:

You can install a gem named pry. You can set that in your view file when you need, so you can get an interactive shell in your server when the server serve that page. There you can test all your dynamic value carriers. And you can understand whats going on actually.

Rubyrider
  • 3,567
  • 1
  • 28
  • 34