No commit activity in last 3 years
No release in over 3 years
redirect with flash helper for Sinatra
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 0.3.0

Runtime

>= 1.0.0
 Project Readme

Sinatra Extension: redirect with flash

Build Status

sinatra-redirect-with-flash provides redirect helper that can set proper flash (rack-flash3 or sinatra-flash) before the redirection.

In fact, every time you set a flash parameter the very next step is often to perform your redirect:

post '/posts/?' do
  @post = Post.create(params)
  flash[:notice] = 'The post was successfully created'
  redirect "/posts/#{@post.id}"
end

With sinatra-redirect-with-flash you can do one-line redirects. For instance, to rewrite the above example:

post '/posts/?' do
  @post = Post.create(params)
  redirect "/posts/#{@post.id}", notice: 'The post was successfully created'
end

##Installation

If you use bundler, simply specify sinatra-redirect-with-flash as a dependency in a Gemfile in your project's root:

gem 'rack-flash3'
gem 'sinatra-redirect-with-flash'

and run bundle install.

Otherwise install the gem as usual:

[sudo] gem install sinatra-redirect-with-flash

##Example

require 'rubygems'
require 'sinatra'
require 'rack-flash'
require 'sinatra/redirect_with_flash'

enable :sessions

post '/sessions/new' do
  redirect '/secret', notice: 'Logged in'
  # predefined keys are: :notice, :error, :warning, :alert, :info, :success
end

get '/foo' do
  redirect '/bar', 301, notice: 'redirect with 301 code'
end

get '/archive' do
  redirect '/posts/', flash: { my_msg: 'Moving on!' }      
end

Note that if your application subclasses Sinatra::Base (modular app), you have to register the extension in your subclass:

helpers Sinatra::RedirectWithFlash

##Requirements

Either rack-flash3 or sinatra-flash.