I’m a beginner in phoenix,
I want to get data from my index.html.eex and insert it in my database with a function in my pageController.
Please help me!!!
Thanks!
I’m a beginner in phoenix,
I want to get data from my index.html.eex and insert it in my database with a function in my pageController.
Please help me!!!
Thanks!
Hello and welcome,
The way it is done is through form. User can fill inputs, click submit, and with some magic, data will be sent to server, sanitized, and persisted into database.
This is not specific to Phoenix.
The road is long, but You can see how people do.
$ mix phx.new demo
$ cd demo
$ mix ecto.create
$ mix phx.gen.html Core Todo todos name:string completed:boolean
* creating lib/demo_web/controllers/todo_controller.ex
* creating lib/demo_web/templates/todo/edit.html.eex
* creating lib/demo_web/templates/todo/form.html.eex
* creating lib/demo_web/templates/todo/index.html.eex
* creating lib/demo_web/templates/todo/new.html.eex
* creating lib/demo_web/templates/todo/show.html.eex
* creating lib/demo_web/views/todo_view.ex
* creating test/demo_web/controllers/todo_controller_test.exs
* creating lib/demo/core/todo.ex
* creating priv/repo/migrations/20210319112810_create_todos.exs
* creating lib/demo/core.ex
* injecting lib/demo/core.ex
* creating test/demo/core_test.exs
* injecting test/demo/core_test.exs
Add the resource to your browser scope in lib/demo_web/router.ex:
resources "/todos", TodoController
Remember to update your repository by running migrations:
$ mix ecto.migrate
You can carefully study the generated files, it will give You an insight of how to manage resources.
accordingly to A.C.Clarke’s third law
Any sufficiently advanced technology is indistinguishable from magic
Thanks,
But I can’t use Form
My data is display in index.html.eex with <%= @myvariable %> and when i click in button or link, it’s get and send to a function in PageController,
How i do this send and how i get this in the function
thank you for your answer
Why not?
@myvariable has been defined in server side, why would You send it back?
Unless You mean to use a link with a parameter… it will be added in the url, and can be retrieved server side.
It’s hard to tell what you are asking, but if you are asking how to populate myvariable from the controller, it’s like this:
def index(conn, _params) do
myvariable = "foo"
render(conn, "index.html", myvariable: myvariable)
end
If you want to send that to the user and then have it come back as input then a hidden field is usually used, but you still need a form element to submit the data back.
By the way, what I wanted to do is add a cart system !!
I have “PRODUITS” and “PANIERS” tables in my database, and I display it in “index.html.eex” and when I click on the “Add cart” button the “pageController” adds the user id and product id in the “PANIERS” table.
This is what I want to do but I can’t retrieve the data in the “index.html.eex” to send it to pageController
Thank you for your help
What did You try so far?
Some think that adding an item to a cart is a patch, so You need to send it with a form.
But You can use a get, and pass the param in the url.
I try to send with Routes.page_path but i have an error
How to add this in the url??
Please show the code and the error stack…
in my “index.html.eex”
<section class="phx-hero">
<h1><%= gettext "Welcome to %{name}!", name: "Phoenix" %></h1>
<p>Peace of mind from prototype to production</p>
</section>
<section class="row">
<article class="column">
<%= for produit <- @produits do%>
<img src="file:///../<%= produit.lienphoto%>" alt="<%= produit.titre %>">
<p>€ <%= produit.prix %>,00</p>
<%= button "Ajout au panier", to: Routes.page_path(@conn, :ajout, produit) %>
<br/>
<% end %>
</article>
<article>
<%= form_for @conn, Routes.session_path(@conn, :delete), [method: :delete, as: :user], fn _ -> %>
<div class="form-group">
<%= submit "logout" %>
</div>
<% end %>
</article>
</section>
<script type ="text/javascript" src = "<%= Routes.static_path(@conn, "/js/app.js") %>"> </script>
in my controller
def requette_inserer_panier(id1,id2) do
params = %{"quantite" => 1, "id_user" => id1, "id_produit" => id2}
Repo.insert(Panier.changeset(%Panier{},params))
end
def ajout(conn, %{"produit" => prod}) do
produits = get_produit()
id_u = Plug.Conn.get_session(conn, :user_id)
id_p = prod.id
requette_inserer_panier(id_u,id_p)
render(conn, "index.html", produits: produits)
end
the error:
Protocol.UndefinedError at GET /page
protocol Enumerable not implemented for %Bbmay.Produit{__meta__: #Ecto.Schema.Metadata<:loaded, "produits">, date: "18/03/2020", id: 4, inserted_at: ~N[2022-05-03 00:00:00], lienphoto: "D:\\Photos\\00020\\PRINCIPAL\\0020-0.JPG", prix: 10, titre: "photo4", updated_at: ~N[2022-05-03 00:00:00]} of type Bbmay.Produit (a struct)
This is wrong…
<%= button "Ajout au panier", to: Routes.page_path(@conn, :ajout, produit: produit) %>
It could have been MUCH easier with the code and error first. It’s hard to guess without any clues.
Thank you very much Kokolegorille
Now i have this error
Phoenix.Router.NoRouteError at POST /pages
no route found for POST /pages (BbmayWeb.Router)
The error is in the router file…
Here is my Router.ex
scope "/", BbmayWeb do
pipe_through :browser
get "/", PageController, :index
get "/page", PageController, :connexion
post "/pages", PageController, :ajout
resources "/users", UserController
resources "/sessions", SessionController, only: [:new, :create, :delete], singleton: true
end
You want to use get, but You set it to post…
But when i use get it send me
ARGUMENT ERROR