ericnormand/playnice

Example with POST params?

Closed this issue · 6 comments

Hi Eric!

Can you give me an example of how the dassoc syntax works with passing on POST params? I'm using the ring.middleware.params/wrap-params middleware (if that's not right, is there something else I should use?)

Thanks in advance!

Follow-up question, which is somewhat related: How do you use playnice.core/dispatch with a Ring handler function? I'm using lein-ring so I specify the handler fn in my project.clj and then that handler threads my stack of middlware through. The issue is that dispatch expects to be inside an app fn, which would have a request parameter, but my handler doesn't have any parameters yet as it is not in a function. Here's what I'm doing:

(def remembrance-handler
  (->
   (playnice/dispatch app-routes)
   (wrap-params)))

As far as the above, I realized I could do

(defn remembrance-handler [req]
  (->
   (playnice/dispatch app-routes req)
   (wrap-params)))

And that seems to work, though I'm not sure about the threading through wrap-params TBH.

Hi Matt,

The defn looks like it's should work. Glad you could find a solution.

What I typically do is something like this:

(def app-routes (-> nil
                    (dassoc "/" homepage)
                    (dassoc "/users/:userid" user-page)))

(defn handler [req]
  (dispatch app-routes req))

(def app
  (-> handler
    (wrap-params)))

Now, if I want to get at the POST params, let's say in user-page, I do:

(defn user-page [req]
  (let [some-post-param (get-in req [:form-params "key"])]
     ....

That will find the post param called "key".

Eric

On Tue, Dec 31, 2013 at 3:40 PM, Matt Gauger notifications@github.comwrote:

As far as the above, I realized I could do

(defn remembrance-handler [req](->
%28playnice/dispatch app-routes req%29
%28wrap-params%29))

And that seems to work, though I'm not sure about the threading through
wrap-params TBH.


Reply to this email directly or view it on GitHubhttps://github.com//issues/1#issuecomment-31412325
.

Thanks for the reply! I'm slowly making my way back to rebuilding my app with playnice, and liking it. I took the README's suggestion of using an atom to store the routes in, but stuck up on how the map built by dassoc-ing onto that map should be passed to the handler.

Here's my routes atom after building them up, according to the REPL:

{1 {index #{{:body hi}}, :type :pathsegment, :variables nil}, :type :pathlength}

Which should mean that "/index" returns a body text of "hi" but not yet seeing that in the browser.

My atom is setup and used in the handler like this:

(def routes (atom {}))

(defn route [path destination]
  (swap! routes
         dassoc
         path
         destination))

;; in my app initialize I call a function which sets an index route, this is called by lein-ring before handler should be wired up, AFAIK

;; then I pass the atom to a handler:
(defn routes-handler [req]
  (playnice/dispatch @routes req))

(def remembrance-handler
  (->
   routes-handler
   (wrap-params)))

Any idea why passing the realized value of the atom to dispatch doesn't seem to be working?

It looks like you've passed a set to your route function as the
destination. The destination should be a Ring handler.

(route "/index" (fn [req] {:body "hi"}))

On Tue, Dec 31, 2013 at 4:04 PM, Matt Gauger notifications@github.comwrote:

Thanks for the reply! I'm slowly making my way back to rebuilding my app
with playnice, and liking it. I took the README's suggestion of using an
atom to store the routes in, but stuck up on how the map built by dassoc-ing
onto that map should be passed to the handler.

Here's my routes atom after building them up, according to the REPL:

{1 {index #{{:body hi}}, :type :pathsegment, :variables nil}, :type :pathlength}

Which should mean that "/index" returns a body text of "hi" but not yet
seeing that in the browser.

My atom is setup and used in the handler like this:

(def routes (atom {}))
(defn route [path destination](swap! routes
dassoc
path
destination))
;; in my app initialize I call a function which sets an index route, this is called by lein-ring before handler should be wired up, AFAIK
;; then I pass the atom to a handler:(defn routes-handler [req](playnice/dispatch @routes req))
(def remembrance-handler
(->
routes-handler
(wrap-params)))


Reply to this email directly or view it on GitHubhttps://github.com//issues/1#issuecomment-31413007
.

Oh wow, now I feel silly! I mixed up anonymous function shorthand and set (#() and #{})

Thanks for all your help! Full steam ahead! I'm tackling replacing some nasty decision-making in my API with Liberator next.