Rails flash messages: is there a good reason they work the way they do? -


the rails 'flash' message system useful, i've found details of implementation make use awkward.

specifically, flash messages cleared @ end of response. means have work out whether they're going used in direct page render or redirect, inform flash system using 'now' if it's not redirect.

this seems overly complex , error-prone.

occasionally find myself building things need exhibit flash-like behaviour, , process use different:

class flashlikestore    attr_accessor :session    def initialize(session)     session[:flashlike] ||= []     self.session = session   end    def add(name, data)     self.store << { name: name, data: data }   end    def read     session.delete(:flashlike).to_json   end    def any?     store && store.any?   end    protected    def store     session[:flashlike]   end  end 

with little syntactic sugar in application helper can add name-value pairs, , act of reading deletes data. (in case i'm reading in via ajaj, isn't important here.)

the upshot messages ever read once, no need up-front determine or guess when they're going appear. they're read when they're needed, go away.

one argue, suppose, call object shouldn't both read data , change state, 'read' method split 'read' , 'wipe' if wanted purist way. (although wouldn't using rails if were.)

so question this: missing something? there compelling reason way rails flash messaging works, need 'now' method?


Comments