我有一个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
的{{1}}方法输入结果。用于计算最终成绩列表的函数称为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还是可以在这里使用Vanilla风味的JVM Clojure?
答案 0 :(得分:1)
您收到错误,因为(process-grades)
正在返回数字向量,这意味着下面的表格
[:p (process-grades (read-string weights) (read-string grades))]
最终会看起来如下(一旦process-grades
返回):
[:p [4/5 3/2 6/3 ... more numbers]]
Hiccup只知道如何在每个打嗝矢量的开头处理关键字html标签,所以它会大声抱怨这个。
最终你需要以你想要的方式很好地格式化输出,但是暂时你应该能够通过在(process-grades ...)
中包含(apply str)
调用来使其运行矢量成一个字符串。