1
votes

I am looking for an extremely simple example of a basic working post response. The application only needs to have two routes/controllers.The first is the homepage that contains an HTML form, and the other is for the embedded forms post response.

Suprisingly all the examples of similar code online are completley bloated (IMHO) with code that I don't understand.

As it stands, the error I am getting is asking for a CSRF token(yes, I know what that is) . In some of the code I've viewed online the forms have a line that looks something like this to generate it:

<input type="hidden" name="csrf_token" value="<%= csrf_token(@conn) %>">

When I add the above line to my form I get an error that says the csrf_token() function is undefined.

So far here is what I have:

HTML

<form action="/create" method="post">
    <input type="text" name="todo">
    <input type="submit">
</form>

router

 scope "/", ProjWeb do
    pipe_through :browser # Use the default browser stack

    get "/", PageController, :index
    post "/create", TodoController, :new
  end

PageController

defmodule ProjWeb.PageController do
  use ProjWeb, :controller

  def index(conn, _params) do
    render conn, "index.html"
  end
end

TodoController

defmodule ProjWeb.TodoController do
  use ProjWeb, :controller
  def new(conn, _params) do
    render conn, "index.html"
  end

end

How do I make the post request work with the code I have so far ?

Thank you

1
What is your question? - Aleksei Matiushkin
Read the post. What do you not understand ? Oh I see, here is my question. How do I make my post request work. The code I posted above is what I have so far. Is that more clear? - William

1 Answers

3
votes

Add this %= Plug.CSRFProtection.get_csrf_token() in the value field and try.