weavejester / hiccup

Fast library for rendering HTML in Clojure
http://weavejester.github.io/hiccup
Eclipse Public License 1.0
2.69k stars 177 forks source link

Ambiguity in using form-to #25

Closed KnowledgeGarden closed 13 years ago

KnowledgeGarden commented 13 years ago

The example: (form-to [method action] uses :put "/post" and suggests that "/post" is a route. It's not clear what that route stands for. If I use defpage "/post", then I get the "missing page" message when http://localhost:8080/post fires based on the form (a login form). If I click refresh, the code fires, but not when called programmatically. What am I missing? Many thanks.

weavejester commented 13 years ago

The method and action arguments fill in the attributes of the same name. For example:

user=> (html (form-to [:get "/route"]))
"<form action=\"/route\" method=\"GET\"></form>"

However, browsers can only handle GET and POSTs from forms. In order to support PUT and DELETE, Hiccup takes a page from Ruby on Rails and Sinatra, and supplies a _method parameter:

user=> (html (form-to [:put "/route"]))
"<form action=\"/route\" method=\"POST\">
<input id=\"_method\" name=\"_method\" type=\"hidden\" value=\"PUT\" />
</form>"

See how the "PUT" is placed in a hidden input type called "_method"?

Routing libraries like Compojure check for this parameter, and use it to override the actual HTTP method. You mentioned you used "defpage", but you do not mention which web framework that comes from.

Aeyk commented 3 years ago

It appears now[1] that browser compatibility for the rest of the HTTP verbs has caught up to RESTful design. Should there be a way to use native browser HTTP verbs when using form-to.?

https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods#browser_compatibility