Error "Not found" compojure.route

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:

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

  3. Browser redirects to

  4. “Not found” displayed instead of updated article


(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))


(ns cljblog.pages
  (:require [ :refer [html5]]
            [hiccup.form :as form]
            [ring.util.anti-forgery :refer [anti-forgery-field]]))

(defn base-page [& body]
   [:head [:title "CljBlog"]]
    [:a {:href "/"} [:h1 "CljBlog"]]
    [:a {:href "/articles/new"} "New article!"]

(defn index [articles]
   (for [a articles]
     [:h2 [:a {:href (str "/articles/" (:_id a))} (:title a)]])))

(defn article [a]
   [:a {:href (str "/articles/" (:_id a) "/edit")} "edit"]
   [:small (:created a)]
   [:h1 (:title a)]
   [:p (:body a)]))

(defn edit-article [a]
    [:post (if a
             (str "/articles/" (:_id a))

     (form/label "title" "Title")
     (form/text-field "title" (:title a))

     (form/label "body" "Body")
     (form/text-area "body" (:body a))


     (form/submit-button "Save!")))


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

(def articles-coll "articles")

(def db (-> db-connection-uri

(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)
             {: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.

I left a comment on SO - seems like there’s a trailing slash inconsistency.

1 Like

Solution by @p-himik :

… your (POST "/articles/:art-id/" ...) route has a trailing slash, while the POST request itself doesn’t

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.