I've been working on a Clojure project for some time and was wondering how to navigate user from one page to another after clicking the submit-button. Code goes like this:
(defn view-film-input []
(view-layout
[:body {:style "background-color: #F2FB78"}
[:h2 "Add new film"]
(form-to [:post "/addfilm" ]
(label "movname" "Film title: ")
(text-field :txtname) [:br]
(label "prname" "Producer name: ")
(text-field :txtprname) [:br]
(label "location" "File location: ")
(text-field :txtlocation)[:br]
(label "duration" "Duration(minutes): ")
(text-field :txtduration)[:br]
(submit-button "Save"))]))
Now, this works but I would like to navigate user to same "Add new film" page, or refresh the form after clicking "Save" button, instead it shows just a blank page.
This is the GET\POST part:
(GET "/addfilm" [] (view-film-input))
(POST "/addfilm" [txtname txtprname txtlocation txtduration]
(insert-flick txtname txtprname txtlocation txtduration 90) )
Thanks in advance!
You have two possibilities here.
Redirect the User
Using the Location header of a HTTP 302 (or 303) response you can specify a path the user's browser should display instead of the current one:
(POST "/addfilm" [...]
...
{:status 302
:headers {"Location" "/addfilm"}})
There are also two functions in ring.util.response that would generate the responses for you: redirect and redirect-after-post - with the latter being more applicable to your use case.
EDIT: This answer elaborates on why 303 would be the status code to send after POST, specifically:
If you use 302, you're risking that UA will re-send POST to the new URL instead of switching to GET.
Render the same Page again
Simpler and prompting less I/O, but imposing some duplication would be to render the same view again, i.e.:
(POST "/addfilm" [...]
...
(view-film-input))
This seems less maintainable to me but it is probably the shortest path to solving your problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With