Hello,
as RESTful API from resources
offers new
and create
routes to create a new user, I don’t know how to use them,
new
will trigger the server to return an HTML form to upload our informations, okay that’s fine, but how we can send those informations back to the server ? and what is the relation with create
route ?
GET /users/new HelloWeb.UserController :new
…
POST /users HelloWeb.UserController :create
…
source: Routing — Phoenix v1.7.1
The :new
route is to get the blank form, hence the GET
HTTP request. While the :create
route is to submit/post the completed form, hence the POST
HTTP request.
If you’re on a relatively new version of Phoenix and ran mix phx.gen.html ...
, you’d see a form component with an action that points to a :create
route.
<.form :let={f} for={@changeset} action={Routes.comment_path(:create, @comment)}> # or action={~p"/comments"} if using verified routes
<.input field={f[:body]} />
</.form>
Thann you @codeanpeace for your answer and sorry for the delay,
In fact what I was mistaken about, is that I thinked for a Framework as Regular RESTful API, that means I expected to render a simple html form by calling new
and after that sending “manually” the informations to the server and this is not the case for Phoenix.
Iam new to Phoenix and I didn’t use LiveView before, what caught my attention is the link that you gave me which describe the form as “regular http request outside of LiveView” so just if you can tell me or suggest a link of how to do that with LiveView ?
Phoenix supports both traditional stateless RESTful HTTP request/response through Phoenix.Controller
as well as stateful WebSocket connections through LiveView Phoenix.Liveview
.
If I’m understanding you correctly, you can just scroll up using the link above for an example of using the form component within a LiveView or alternatively use this link. You can also use the mix phx.gen.live task to generate functional LiveView example code.