From a3b251449b1a6c266fd07eaeeeb7e141abe9673b Mon Sep 17 00:00:00 2001 From: Steven Deobald Date: Tue, 15 Dec 2020 15:48:00 -0600 Subject: [PATCH 1/2] Downcase hidden methods in RESTful example - this documentation is mildly confusing when combined with hiccup's `form-to` since hiccup forcibly transforms the method specified in its convenience syntax: `(form-to [:delete "/my-url"] ... )` into an upper-case string: https://github.com/weavejester/hiccup/blob/80e48352dd2bce5277ccdd06334cfb9be915afb4/src/hiccup/form.clj#L130 - it's also common to get an upper-case string from elsewhere so it seems best to wrap the hidden `_method` in `lower-case`. --- doc/ring/RESTful_form_methods.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/doc/ring/RESTful_form_methods.md b/doc/ring/RESTful_form_methods.md index 6ab793b5..d451275c 100644 --- a/doc/ring/RESTful_form_methods.md +++ b/doc/ring/RESTful_form_methods.md @@ -8,9 +8,10 @@ We can do this with middleware in reitit like this: ```clj (defn- hidden-method [request] - (keyword - (or (get-in request [:form-params "_method"]) ;; look for "_method" field in :form-params - (get-in request [:multipart-params "_method"])))) ;; or in :multipart-params + (keyword + (clojure.string/lower-case + (or (get-in request [:form-params "_method"]) ;; look for "_method" field in :form-params + (get-in request [:multipart-params "_method"]))))) ;; or in :multipart-params (def wrap-hidden-method {:name ::wrap-hidden-method From 7fb720ef36ebb6158d5168d1c53d838a2d5b06d0 Mon Sep 17 00:00:00 2001 From: Steven Deobald Date: Tue, 15 Dec 2020 19:29:50 -0600 Subject: [PATCH 2/2] Handle nil for `lower-case` - when "_method" is actually empty, we need to avoid trying to do string manipulation on nil from params. --- doc/ring/RESTful_form_methods.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/ring/RESTful_form_methods.md b/doc/ring/RESTful_form_methods.md index d451275c..5faaeb3c 100644 --- a/doc/ring/RESTful_form_methods.md +++ b/doc/ring/RESTful_form_methods.md @@ -8,10 +8,10 @@ We can do this with middleware in reitit like this: ```clj (defn- hidden-method [request] - (keyword - (clojure.string/lower-case - (or (get-in request [:form-params "_method"]) ;; look for "_method" field in :form-params - (get-in request [:multipart-params "_method"]))))) ;; or in :multipart-params + (some-> (or (get-in request [:form-params "_method"]) ;; look for "_method" field in :form-params + (get-in request [:multipart-params "_method"])) ;; or in :multipart-params + clojure.string/lower-case + keyword)) (def wrap-hidden-method {:name ::wrap-hidden-method