ユーザー入力を整数のベクトルhome
として要求するレンダリング中のページがあります。入力を操作するために使用する数学関数とうまく連携するため、これらのデータ構造が必要です。
(defn home [& [weights grades error]]
(html
;; ...
(form-to [:post "/"]
;; ...
(text-area {:rows 15 :cols 30 :placeholder
"[89 78 63]
[78 91 60]
[87 65 79]
..." } "grades" grades)]
(submit-button "process"))]))
そして、"process"
ボタンはメソッドdefroutes
を使用して関数を介して入力を送信します。このメソッドは、入力から計算された結果を表示するメソッドPOST
を呼び出します。成績の最終的なリストを計算するために使用される関数は、 と呼ばれます。入力データ構造を関数で処理できるものに変更しようとしていますが、機能させることはできません。への呼び出しを に置き換えると、ボタンを押した後のテキストのレンダリングに問題はありません。processed
html
process-grades
read-string
processed
"TEST"
process
(defn process-grades
"Takes user input from home's form-to function and processes it into the final grades list"
[weights grades]
(->> grades
(map (partial percentify-vector weights))
(mapv #(apply + %))))
(defn processed [weights grades]
(cond
(empty? weights)
(home weights grades "You forgot to add the weights!")
(empty? grades)
(home weights grades "You forgot to add the grades!")
:else
(do
(html
[:h2 "These are your final grades."]
[:hr]
[:p (process-grades (read-string weights)(read-string grades))])))) ;; <- This is not working!
(defroutes grade-routes
(GET "/" []
{:status 200
:headers {"Content-Type" "text/html"}
:body (home)
})
(POST "/" [weights grades] (processed weights grades))
(ANY "*" []
(route/not-found (slurp (io/resource "404.html")))))
html
form
タグ、Clojure のread-string
機能、および必要な機能をスクリプト化するさまざまな方法について少し調査しました。情報があふれているので、私はまだ疑問に思っています。これを行うための最も簡単で、最も簡潔で、慣用的な方法は何ですか? Clojurescript を使用する必要がありますか、それともバニラ風味の JVM Clojure をここで使用できますか?