Skip to content

Instantly share code, notes, and snippets.

@katafrakt
Created January 25, 2019 08:42
Show Gist options
  • Save katafrakt/50e3870f8a73e85ca56011458d1099d2 to your computer and use it in GitHub Desktop.
Save katafrakt/50e3870f8a73e85ca56011458d1099d2 to your computer and use it in GitHub Desktop.
Problem with flash in Hanami
require 'bundler/inline'
gemfile do
gem 'hanami', '~> 1.3'
end
require 'hanami/action/session'
module Home
class Index
include Hanami::Action
include Hanami::Action::Session
def call(_params)
flash[:errors] = 'WRONG!'
puts "In index"
p flash
redirect_to '/redirected'
end
end
class Redirected
include Hanami::Action
include Hanami::Action::Session
def call(_params)
puts "In redirected"
p flash
self.body = flash[:errors]
end
end
end
module FlashApp
class Application
attr_reader :routes
def initialize
@routes = Hanami::Router.new do
get '/', to: 'home#index'
get '/redirected', to: 'home#redirected'
end
end
end
end
app = Rack::Builder.new do
use Rack::Session::Cookie, secret: SecureRandom.hex(16)
run FlashApp::Application.new.routes
end
Rack::Server.start app: app
curl -L localhost:8080
@katafrakt
Copy link
Author

Result:

[2019-01-25 09:41:14] INFO  WEBrick 1.4.2
[2019-01-25 09:41:14] INFO  ruby 2.5.1 (2018-03-29) [x86_64-linux]
[2019-01-25 09:41:14] INFO  WEBrick::HTTPServer#start: pid=15276 port=8080
In index
#<Hanami::Action::Flash:0x560d5caf45a0 {:data=>{:errors=>"WRONG!"}, :kept=>{}} >
::1 - - [25/Jan/2019:09:41:17 CET] "GET / HTTP/1.1" 302 5
- -> /
In redirected
#<Hanami::Action::Flash:0x560d5cad21a8 {:data=>{}, :kept=>{}} >
::1 - - [25/Jan/2019:09:41:17 CET] "GET /redirected HTTP/1.1" 200 0
- -> /redirected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment