I have the following button that handles “handle_login”, its in my testweb/templates/admin/admin.html.heex, where do I have to create a elixir file with the following function to actually handle it:
def handle_click("handle_login"), do: ...
I tried to put it into the same folder as the html file (admin.ex), but when I clicked the button, nothing happened (I added a IO.puts to log something but nothing happend
Hi @spizzy yes phx-click is liveview only. For a login form, I would consider just doing a normal HTML form and having a route that handles the POST when the form is submitted.
I have a page where a user has to enter a generated password that is saved in a database.
When the user enters his password, I wanted to call a .ex file that runs through several functions for Ecto etc. and returns an error message or success message …
OK. If you use the generators like mix phx.gen.html it will generate some common templates, controllers and forms. There is also mix phx.gen.auth which will generate a login form. If you aren’t sure how to hook up a form to a controller I’d also consider one of the more detailed guides or books that walks you through it step by step.
I meant as a way to learn how it works. Make a new git branch on your project, generate some stuff, play with it, and then delete the branch if you like.
To a controller. I’m happy to keep helping but this would be a lot more productive if you’d read the link I sent, try to write the code out, and then come back with specific questions about what you tried and what went wrong. I don’t think I’m going to be able to give you a tutorial on HTML and Phoenix that is better than the Phoenix book or better than the link I sent.
def create(conn, _params) do
if conn.params["password"] == "something" do
conn
|> put_flash(:info, "You are now logged in")
|> redirect(to: "/kek")
else
conn
|> put_flash(:error, "Wrong password")
|> redirect(to: "/parents")
end
end
why is this saying (when I enter something in the form) no route found for POST /%7Badmin_path(@conn,%20:create)%7D (AdminWeb.Router)
Looks like you’re going the Controllers route. Take a look at the documentation.
You pretty much need this in your router:
scope "/", HelloWeb do
pipe_through :browser
get "/admin/login", SessionController, :new # This renders the form, you should have something similar working
post "/admin/login", SessionController, :create # This handles the submit
end
And then a controller that handles the new/create actions. Whatever you use in your post route is the action you need in your form. I used “/admin/login”, you used only “/admin”, change as needed.
Also reading your posts above, this piece of code is wrong (you can confirm by inspecting element in your page and cheking the generated HTML code):
<form class="phx-hero" action="{admin_path(@conn, :create)}" method="post">
# This renders action="{admin_path(@conn, :create)}" in the HTML
It should be:
<form class="phx-hero" action={Routes.admin_path(@conn, :create)} method="post">
# This renders action="/admin/login" or whatever is in your router config in the HTML
The difference is that you have the {} within the "" and it is evaluated as a string.