错误:“找不到” clojure,compojure.Route

发布于 2025-01-31 16:13:02 字数 3443 浏览 0 评论 0原文

我在博客应用程序中有一个更新文章路线。 它应提交带有更新的帖子参数的发布请求,然后重定向到新更新的帖子本身。

取而代之的是,它重定向到一个“找不到的”页面,其中包括文章ID在内的URI与原始文章的URI相同。

采取的步骤

  1. 请点击链接到编辑页面: Localhost:3000/actits/4/edit

  2. 更新帖子,并使用保存按钮提交更改。

  3. 浏览器重定向到 http // localhost:3000/actity/4

  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

  1. Follow link to edit page:
    localhost:3000/article/4/edit

  2. Update post, and submit changes with save button.

  3. Browser redirects to
    http//localhost:3000/article/4

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文