1

我有一个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?

4

1 回答 1

1

你得到一个错误,因为(process-grades)它返回一个数字向量,这意味着下面的表格

[:p (process-grades (read-string weights) (read-string grades))]

最终将如下所示(一旦process-grades返回):

[:p [4/5 3/2 6/3 ... more numbers]]

Hiccup 只知道如何处理每个 hiccup 向量开头的关键字 html 标签,所以它会大声抱怨这个。

最终,您需要以您想要的方式很好地格式化输出,但目前您应该能够通过将(process-grades ...)调用包装起来(apply str)以将向量转换为字符串来使其运行。

于 2014-02-27T11:26:36.707 回答