1

我正在制作一个 Clojure 网络应用程序来处理数字输入数据。我已经设置了主页,并且正在处理输入的数据并在通过按钮提交后显示。

带有注释的关键代码位:

(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))  ;; <- The functions being called here are omitted but defined in the same name space.
         (mapv #(apply + %))))


    (defn home [& [weights grades error]]
  (html
    [:head
    [:title "Home | Clojuregrade"]]
    [:body
    [:h1 "Welcome to Clojuregrade"]
    [:p error]
    [:hr]
   (form-to [:post "/"]
    [:h3 "Enter the weights for each of the grades below. Of course, all of the numbers should add up to 100%. Be sure to include the brackets"
    [:br]
     (text-area {:cols 30 :placeholder "[40 10 50] <- adds up to 100%"} "weights" weights)]
    [:h3 "Enter ALL of the grades for EACH STUDENT in your class.
      Make sure that each of the grades is ordered such that the grade corresponds
      to its matching weight above."
    [:br]
    (text-area {:rows 15 :cols 30 :placeholder
"[89 78 63]
                    [78 91 60]
                    [87 65 79]
                    ... " } "grades" grades)]
     (submit-button "process"))]))

(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 "test"]))))  ;; <- I would like to call process-grades here. "test" renders fine. 

(defroutes app
  (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"))))) 

;; ... 
4

2 回答 2

1

我的假设是正确的。我正在使用您的代码并将其放入准系统 luminus 项目中。您的代码生成的 html 如下所示:

<h3>

Enter the weights for each of the grades below. Of…
<br></br>
<textarea id="weights" placeholder="[40 10 50] <- adds up to 100%" name="weights" cols="30"></textarea>
</h3>
<h3>
Enter ALL of the grades for EACH STUDENT in your c…
<br></br>
<textarea id="grades" rows="15" placeholder="bla" name="grades" cols="30"></textarea>
</h3>

错误来了:

<form method="POST" action="/">
    <input type="submit" value="process"></input>
</form>

表单元素必须包装输入元素,否则它不会将它们作为请求的一部分提交。

因此,如果您将打嗝代码更改为:

(defn home [ & [weights grades error]]
  (html
    [:h1 "Welcome to Clojure-grade"]
    [:p error]
    [:hr]
    (form-to [:post "/"]
      [:h3 "Enter the weights for each of the grades below. Of course, all of the numbers should add up to 100%. Be sure to include the brackets"
     [:br]
     (text-area {:cols 30 :placeholder "[40 10 50] <- adds up to 100%"} "weights" weights)
     ]
    [:h3 "Enter ALL of the grades for EACH STUDENT in your class.
      Make sure that each of the grades is ordered such that the grade corresponds
      to its matching weight above."
     [:br]
     (text-area {:rows 15 :cols 30 :placeholder "bla"} "grades" grades)]
                                       ;...
                                       ;
                                       ;                                     (Each     grade corresponds to one of the weights above,
                                       ; so order is important. You can copy and paste directly from your excel file but don't forget
                                       ; the brackets!)" } "grades" grades)]
     (submit-button "process")))) ;; <- when I hit this in the browser I get a 404, with or without input.

有用。仔细看看:“(form-to [:post“/”]”。我将它向上移动了几行,以便它包裹输入元素。

顺便说一句,花一些时间使用浏览器开发工具并阅读至少一个小的 html 教程。尤其是知道如何使用开发人员工具将在调试此类问题时为您提供很大的优势。

于 2014-02-25T19:49:09.290 回答
0

在哪里weightsgrades被传递到processed哪里?如果你想使用它们,我认为你需要将它们作为参数传递:

(defn processed [weights grades]
  ;; .. snip
  )

;; .. snip

(POST "/" [weights grades] (processed weights grades))
于 2014-02-25T06:20:33.923 回答