0.2.3 permalink Arrow_down_16x16
  • (wrap-flash handler)
If a :flash key is set on the response by the handler, a :flash key with
the same value will be set on the next request that shares the same session.
This is useful for small messages that persist across redirects.

1 Example top

  • (use 'ring.middleware.flash
         'ring.middleware.params
         'ring.middleware.session
         'ring.util.response
         'ring.adapter.jetty)
    
    (defn save
      [request]
      (let [value (-> request :params :value)]
        ;; somehow save value
        ;; the :flash key on the response will be placed in the :session by wrap-flash
        (assoc (redirect "/") :flash "Value is saved")))
    
    (defn home
      [request]
      (let [uri (-> request :uri)]
        (if (.startsWith uri "/save")
           (save request)
           ;; before calling the home fn wrap-flash will move the :flash message from the :session to the request
           (if-let [flash-message (-> request :flash)]
              (response (str "Flash message found: " flash-message))
              (response "No flash message found")))))
    
    (def app (-> home wrap-flash wrap-session wrap-params))
    
    (run-jetty app {:port 3000})
    
    ;; go to http://localhost:3000 and http://localhost:3000/save
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source ring/middleware/flash.clj:4 top

(defn wrap-flash
  "If a :flash key is set on the response by the handler, a :flash key with
  the same value will be set on the next request that shares the same session.
  This is useful for small messages that persist across redirects."
  [handler]
  (fn [request]
    (let [session  (request :session)
          flash    (session :_flash)
          session  (dissoc session :_flash)
          request  (assoc request
                     :session session
                     :flash flash)
          response (handler request)
          session  (if-let [flash (response :flash)]
                     (assoc (response :session) :_flash flash)
                     session)]
      (assoc response :session session))))
Vars in ring.middleware.flash/wrap-flash: assoc defn fn let
Used in 0 other vars

Comments top

No comments for wrap-flash. Log in to add a comment.