【发布时间】:2018-10-17 23:57:39
【问题描述】:
我正在构建我的第一个 Web 应用程序,当用户 submits 表单时,我很难访问表单的各个字段。这是我所拥有的:
(defroutes app
(GET "/" [] homepage)
(POST "/city" request display-city)
(route/resources "/")
(route/not-found "Not Found"))
(defn display-city [request]
(html5
[:div {:class "the-city"}
[:h2 "ALL ABOUT YOUR CITY"]
[:ul
[:li "Your city is " (str request) "! That's all"]]]))
;; and here's the hiccup form:
[:form {:action "/city" :method "post"}
(anti-forgery-field)
[:p "Enter your home address"]
[:div
[:label {:for "street-field"} "Street:"]
[:input {:id "street-field"
:type "text"
:name "street"}]]
[:div
[:label {:for "city-field"} "City:"]
[:input {:id "city-field"
:type "text"
:name "city"}]
[:div
[:label {:for "state-field"} "State:"]
[:input {:id "state-field"
:type "text"
:name "state"}]
[:label {:for "zip-field"} "ZIP:"]
[:input {:id "zip-field"
:type "text"
:name "zip"
:size "10"}]]
[:div.button
[:button {:type "submit"} "Submit"]]]])
;;当我运行上面的代码时,我可以看到通过(str request) 提交的整个表单,看起来像是一个 Clojure 映射。但是我不知道如何提取单个“key/vals”(从那个地址表单中,我想提取城市),或者如何以我可以使用的方式存储这些结果。有什么想法吗?
这是一个超级基本的/city 页面,我试图在构建更大的东西之前了解它是如何工作的。谢谢!
【问题讨论】:
-
我不明白这个问题。什么是“城市”?请展示输入数据、输出数据、您尝试过的内容以及发生的情况的示例。
标签: clojure compojure ring hiccup