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を呼び出します。成績の最終的なリストを計算するために使用される関数は、 と呼ばれます。入力データ構造を関数で処理できるものに変更しようとしていますが、機能させることはできません。への呼び出しを に置き換えると、ボタンを押した後のテキストのレンダリングに問題はありません。processedhtmlprocess-gradesread-stringprocessed"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 に答える