Changes for ring.middleware.flash/wrap-flash

sdevijver on Wed, 30 May 2012
(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
sdevijver on Tue, 29 May 2012
(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
    (assoc (redirect "/") :flash "Value is saved")))

(defn home
  [request]
  (let [uri (-> request :uri)]
    (if (.startsWith uri "/save")
       (save 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