Module: Ramaze::Helper::Flash

Defined in:
lib/ramaze/helper/flash.rb

Overview

The purpose of this class is to provide an easy way of setting/retrieving from the current flash.

Flash is a way to keep a temporary pairs of keys and values for the duration of two requests, the current and following.

Very vague Example:

On the first request, for example on registering:

flash[:error] = "You should reconsider your username, it's taken already"
redirect R(self, :register)

This is the request from the redirect:

do_stuff if flash[:error]

On the request after this, flash is gone.

Instance Method Summary collapse

Instance Method Details

#flashObject

answers with Session.current.flash



29
30
31
# File 'lib/ramaze/helper/flash.rb', line 29

def flash
  Session.current.flash
end

#flashbox(tag = ancestral_trait[:flashbox]) ⇒ Object

Use in your template to display all flash messages that may be stored. For example, given you stored:

flash # => { :error => 'Pleae enter your name'
             :info => 'Do you see the fnords?' }

Then a flashbox would display:

<div class='flash' id='flash_error'>Please enter your name</div>
<div class='flash' id='flash_info'>Do you see the fnords?</div>

This can be customized permanently in your controller:

class MainController < Ramaze::Controller
  trait :flashbox => "<div class='flash_%key'>%value</div>"

  def show_flashbox
    flashbox
  end
end

or on a per-usage basis in your action:

class MainController < Ramaze::Controller
  def show_flashbox
    flashbox("<div class='flash_%key'>%value</div>")
  end
end

Where any occurrence of %key and %value will be replaced by the actual contents of each element of flash



66
67
68
69
70
71
72
# File 'lib/ramaze/helper/flash.rb', line 66

def flashbox(tag = ancestral_trait[:flashbox])
  flash.map{|key, *values|
    values.flatten.map do |value|
      tag.gsub(/%key/, key.to_s).gsub(/%value/, value.to_s)
    end
  }.flatten.join("\n")
end