错误:“找不到” clojure,compojure.Route
我在博客应用程序中有一个更新文章路线。 它应提交带有更新的帖子参数的发布
请求,然后重定向到新更新的帖子本身。
取而代之的是,它重定向到一个“找不到的”页面,其中包括文章ID在内的URI与原始文章的URI相同。
采取的步骤
请点击链接到编辑页面: Localhost:3000/actits/4/edit
更新帖子,并使用保存按钮提交更改。
浏览器重定向到 http // localhost:3000/actity/4
显示“未找到”而不是更新的文章
src/cljblog/handler.clj
(ns cljblog.handler
(:require [compojure.core :refer :all]
[compojure.route :as route]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]
[ring.util.response :as resp]
[cljblog.db :as db]
[cljblog.pages :as p]))
(defroutes app-routes
(GET "/" [] (p/index (db/list-articles)))
(GET "/articles/new" [] (p/edit-article nil))
(POST "/articles" [title body]
(do (db/create-article title body)
(resp/redirect "/")))
(GET "/articles/:art-id" [art-id] (p/article (db/get-article-by-id art-id)))
(GET "/articles/:art-id/edit" [art-id] (p/edit-article (db/get-article-by-id art-id)))
(POST "/articles/:art-id/" [art-id title body]
(do (db/update-article art-id title body)
(resp/redirect (str "/articles/" art-id))))
(route/not-found "Not Found"))
(def app
(wrap-defaults app-routes site-defaults))
src/cljblog/pages.clj
(ns cljblog.pages
(:require [hiccup.page :refer [html5]]
[hiccup.form :as form]
[ring.util.anti-forgery :refer [anti-forgery-field]]))
(defn base-page [& body]
(html5
[:head [:title "CljBlog"]]
[:hr]
[:body
[:a {:href "/"} [:h1 "CljBlog"]]
[:a {:href "/articles/new"} "New article!"]
body]))
(defn index [articles]
(base-page
(for [a articles]
[:h2 [:a {:href (str "/articles/" (:_id a))} (:title a)]])))
(defn article [a]
(base-page
[:a {:href (str "/articles/" (:_id a) "/edit")} "edit"]
[:hr]
[:small (:created a)]
[:h1 (:title a)]
[:p (:body a)]))
(defn edit-article [a]
(base-page
(form/form-to
[:post (if a
(str "/articles/" (:_id a))
"/articles")]
(form/label "title" "Title")
(form/text-field "title" (:title a))
(form/label "body" "Body")
(form/text-area "body" (:body a))
(anti-forgery-field)
(form/submit-button "Save!")))
)
src/cljblog/db.clj
(ns cljblog.db
(:require [monger.core :as mg]
[monger.collection :as mc]
[monger.operators :refer [$set]])
(:import [org.bson.types ObjectId]))
(def db-connection-uri (or (System/getenv "CLJBLOG_MONGO_URL")
"mongodb://127.0.0.1/cljblog-test"))
(def articles-coll "articles")
(def db (-> db-connection-uri
mg/connect-via-uri
:db))
(defn create-article [title body]
(mc/insert db articles-coll
{:title title
:body body
:created (new java.util.Date)}))
(defn update-article [art-id title body]
(mc/update-by-id db articles-coll (ObjectId. art-id)
{$set
{:title title
:body body}}))
(defn list-articles []
(mc/find-maps db articles-coll))
(defn get-article-by-id [art-id]
(mc/find-map-by-id db articles-coll (ObjectId. art-id))
)
*重定向在应用程序中其他位置的工作。
I have an update article route in a blog application.
It should submit a POST
request with the updated post params then redirect to the newly updated post itself.
Instead it redirects to a "Not found" page where the URI including the article's id is identical to the URI of the of the original article.
Steps taken
Follow link to edit page:
localhost:3000/article/4/editUpdate post, and submit changes with save button.
Browser redirects to
http//localhost:3000/article/4"Not found" displayed instead of updated article
src/cljblog/handler.clj
(ns cljblog.handler
(:require [compojure.core :refer :all]
[compojure.route :as route]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]
[ring.util.response :as resp]
[cljblog.db :as db]
[cljblog.pages :as p]))
(defroutes app-routes
(GET "/" [] (p/index (db/list-articles)))
(GET "/articles/new" [] (p/edit-article nil))
(POST "/articles" [title body]
(do (db/create-article title body)
(resp/redirect "/")))
(GET "/articles/:art-id" [art-id] (p/article (db/get-article-by-id art-id)))
(GET "/articles/:art-id/edit" [art-id] (p/edit-article (db/get-article-by-id art-id)))
(POST "/articles/:art-id/" [art-id title body]
(do (db/update-article art-id title body)
(resp/redirect (str "/articles/" art-id))))
(route/not-found "Not Found"))
(def app
(wrap-defaults app-routes site-defaults))
src/cljblog/pages.clj
(ns cljblog.pages
(:require [hiccup.page :refer [html5]]
[hiccup.form :as form]
[ring.util.anti-forgery :refer [anti-forgery-field]]))
(defn base-page [& body]
(html5
[:head [:title "CljBlog"]]
[:hr]
[:body
[:a {:href "/"} [:h1 "CljBlog"]]
[:a {:href "/articles/new"} "New article!"]
body]))
(defn index [articles]
(base-page
(for [a articles]
[:h2 [:a {:href (str "/articles/" (:_id a))} (:title a)]])))
(defn article [a]
(base-page
[:a {:href (str "/articles/" (:_id a) "/edit")} "edit"]
[:hr]
[:small (:created a)]
[:h1 (:title a)]
[:p (:body a)]))
(defn edit-article [a]
(base-page
(form/form-to
[:post (if a
(str "/articles/" (:_id a))
"/articles")]
(form/label "title" "Title")
(form/text-field "title" (:title a))
(form/label "body" "Body")
(form/text-area "body" (:body a))
(anti-forgery-field)
(form/submit-button "Save!")))
)
src/cljblog/db.clj
(ns cljblog.db
(:require [monger.core :as mg]
[monger.collection :as mc]
[monger.operators :refer [$set]])
(:import [org.bson.types ObjectId]))
(def db-connection-uri (or (System/getenv "CLJBLOG_MONGO_URL")
"mongodb://127.0.0.1/cljblog-test"))
(def articles-coll "articles")
(def db (-> db-connection-uri
mg/connect-via-uri
:db))
(defn create-article [title body]
(mc/insert db articles-coll
{:title title
:body body
:created (new java.util.Date)}))
(defn update-article [art-id title body]
(mc/update-by-id db articles-coll (ObjectId. art-id)
{$set
{:title title
:body body}}))
(defn list-articles []
(mc/find-maps db articles-coll))
(defn get-article-by-id [art-id]
(mc/find-map-by-id db articles-coll (ObjectId. art-id))
)
*redirects work as intended elsewhere in the app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论