LoginSignup
0
0

More than 5 years have passed since last update.

ataraxyでredirectする

Last updated at Posted at 2018-01-27

改訂版

コメントでより良い書き方を教えていただいたのでそちらで書き直しました。
ring.util.responseではなく、ataraxy.responseを利用してredirectをする方法です。
https://github.com/weavejester/ataraxy/blob/0.4.0/src/ataraxy/response.clj#L50-L66

(ns foo.root
  (:require [ataraxy.response :as response]
            [integrant.core :as ig]))

(defmethod ig/init-key ::index [_ options]
  (fn [{[_] :ataraxy/result}]
    [::response/found "/bar"]))

ここから以前の実装方法

サンプル

こんな感じです。

(ns foo.root
  (:require [ataraxy.response :as response]
            [integrant.core :as ig]
            [ring.util.response :refer [redirect]]))

(defmethod ig/init-key ::index [_ options]
  (fn [{[_] :ataraxy/result}]
    (redirect "/bar")))

ちなみにredirectは↓みたいなMapを返す関数のようです。

{:status 302, :headers {"Location" "/bar"}, :body ""}

間違い

最初はうっかり↓みたいに書いて500エラーになりました。

;; ダメな例
(defmethod ig/init-key ::index [_ options]
  (fn [{[_ q] :ataraxy/result}]
    ;; response/okは要らなかった
    [::response/ok (redirect "/bar")]))
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0