Git Product home page Git Product logo

rack-flash's Introduction

Rack Flash

flash[:notice] = "You can stop rolling your own now."

Simple flash hash implementation for Rack apps.

View the RDoc.

Usage

Here's how to use it.

Install / add to Gemfile

gem 'rack-flash3'

Vanilla Rack apps

You can access flash entries via env['x-rack.flash']. You can treat it either like a regular flash hash:

env['x-rack.flash'][:notice] = 'You have logged out.'

Or you can pass the :accessorize option to declare your flash types. Each of these will have accessors defined on the flash object:

use Rack::Flash, :accessorize => [:notice, :error]

# Set a flash entry
env['x-rack.flash'].notice = 'You have logged out.'

# Get a flash entry
env['x-rack.flash'].notice # => 'You have logged out.'

# Set a a flash entry for only the current request
env['x-rack.flash'].notice! 'You have logged out.'

Sample rack app:

get = proc { |env|
  [200, {},
    env['x-rack.flash'].notice || 'No flash set. Try going to /set'
  ]
}

set = proc { |env|
  env['x-rack.flash'].notice = 'Hey, the flash was set!'
  [302, {'Location' => '/'},
    'You are being redirected.'
  ]
}

builder = Rack::Builder.new do
  use Rack::Session::Cookie
  use Rack::Flash, :accessorize => true

  map('/set') { run set }
  map('/')    { run get }
end

Rack::Handler::Mongrel.run builder, :Port => 9292

Sinatra

If you're using Sinatra, you can use the flash hash just like in Rails:

require 'sinatra/base'
require 'rack-flash'

class MyApp < Sinatra::Base
  enable :sessions
  use Rack::Flash

  post '/set-flash' do
    # Set a flash entry
    flash[:notice] = "Thanks for signing up!"
    
    # Get a flash entry
    flash[:notice] # => "Thanks for signing up!"
    
    # Set a flash entry for only the current request
    flash.now[:notice] = "Thanks for signing up!"
  end
end

If you've got any ideas on how to simplify access to the flash hash for vanilla Rack apps, let me know. It still feels a bit off to me.

Sweeping stale entries

By default Rack::Flash has slightly different behavior than Rails in that it doesn't delete entries until they are used. If you want entries to be cleared even if they are not ever accessed, you can use the :sweep option:

use Rack::Flash, :sweep => true

This will sweep stale flash entries, whether or not you actually use them.

rack-flash's People

Contributors

akahn avatar jeremyevans avatar knzconnor avatar nakajima avatar neaf avatar treeder avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rack-flash's Issues

Use rack-flash with sinatra test environment

I'm developing a Sinatra application using cucumber.

In my env.rb cucumber configuration file, I have specified:

set :environment, :test

This gives me an error:

Rack::Flash::SessionUnavailable - Rack::Flash depends on session middleware

If you could tell me what's causing that, or how to get round it - would be much appreciated.

Am using:
cucumber (0.4.4)
rack (1.0.1)
rack-flash (0.1.1)
rack-test (0.5.3)
sinatra (0.9.4)

Thanks,
Steven

Sinatra Multiple Files error

Hi I've just been developing an app in sinatra and just moved my authentication logic to another file, but when i tried to use flash[:notice] from inside that file it is nil al the way. It still works fine for the main file though. It's weird. I just installed sinatra-flash to check if it was me, but this works fine.

Thnx

Better doc for tests

A minimal note on Readme file to test the flash value would be great.
I found this way works for me (minitest, rack-test)

assert_equal 'my message', last_request.env['x-rack.flash'][:alert]

flash[:notice] unset after a redirect

Hi,

In my sinatra app, I would like to be able to set a flash[:notice] message, then redirect, but it seems that the flash[:notice] is empty after a redirect.

Is this the expected behaviour?

Thanks,
Steven

Can't check for empty flash

I've been looking for a simple way to check if the flash is empty, but it does not provide any simple array/hash methods for that.

Maybe adding an empty? method to the class that returns true on empty values.

Any ideas on how to do that right now?

undefined method '<<' for nil:NilClass

It does not seem to be working with Sinatra 1.3.2

When just requiring rack-flash it throws this error:

ERROR NoMethodError: undefined method `<<' for nil:NilClass
    /Users/david/.rvm/gems/ruby-1.9.3-p0/gems/rack-flash-0.1.2/lib/rack/flash.rb:11:in `use'
    /Users/david/.rvm/gems/ruby-1.9.3-p0/gems/sinatra-1.3.2/lib/sinatra/base.rb:1339:in `setup_default_middleware'
    /Users/david/.rvm/gems/ruby-1.9.3-p0/gems/sinatra-1.3.2/lib/sinatra/base.rb:1327:in `build'
    /Users/david/.rvm/gems/ruby-1.9.3-p0/gems/sinatra-1.3.2/lib/sinatra/base.rb:1321:in `new'
    /Users/david/.rvm/gems/ruby-1.9.3-p0/gems/sinatra-1.3.2/lib/sinatra/base.rb:1311:in `prototype'
    /Users/david/.rvm/gems/ruby-1.9.3-p0/gems/sinatra-1.3.2/lib/sinatra/base.rb:1334:in `block in call'
    /Users/david/.rvm/gems/ruby-1.9.3-p0/gems/sinatra-1.3.2/lib/sinatra/base.rb:1416:in `synchronize'
    /Users/david/.rvm/gems/ruby-1.9.3-p0/gems/sinatra-1.3.2/lib/sinatra/base.rb:1334:in `call'
    /Users/david/.rvm/gems/ruby-1.9.3-p0/gems/rack-1.4.0/lib/rack/handler/webrick.rb:59:in `service'
    /Users/david/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
    /Users/david/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
    /Users/david/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

Doesn't work with JSON session cookies

Flash keys are automatically converted to symbols. However symbols are not supported by JSON session cookies.

require 'sinatra/base'
require 'rack/flash'

class Server < Sinatra::Application
    configure do
        use Rack::Session::Cookie, coder: Rack::Session::Cookie::Base64::JSON.new
        use Rack::Flash 
    end

    get '/' do
        flash['foo'] = 'bar'
    end
end

Server.run!

Running this sample code produces the error:

Rack::Utils::OkJson::Error at /
Hash key is not a string: :foo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.