Error in template: in form_for phoenix don't see _path function

forms
templates
routerhelpers
#1

I observe some strange behavior of form_for helper in my template. Could somebody help me?

#error:
== Compilation error in file lib/webtestapp_web/views/payment_view.ex ==
** (CompileError) lib/webtestapp_web/templates/payment/index.html.eex:1: undefined function payment_path/2
...

#mix phx.routes
page_path  GET   /              WebtestappWeb.PageController :index
payment_path  GET   /payments/new  WebtestappWeb.PaymentController :new
payment_path  POST  /payments      WebtestappWeb.PaymentController :create

# index.html.eex
...
<%= form_for @conn, payment_path(@conn, :create), [as: :params], fn f -> %>
...

# router.ex
...
resources "/payments", PaymentController, only: [:new, :create]
...

#payment_controller.ex
defmodule WebtestappWeb.PaymentController do
  use WebtestappWeb, :controller

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

# payment_view.ex
defmodule WebtestappWeb.PaymentView do
  use WebtestappWeb, :view
end
0 Likes

#2

Is that Phoenix 1.4? If so You need to prepend Routes before the path.

0 Likes

#3

Yes, It’s Phoenix 1.4. Could you be more specific, please - where should I prepend routes?

0 Likes

#4

Replace with…

<%= form_for @conn, Routes.payment_path(@conn, :create), [as: :params], fn f -> %>

You can see upgrade guide here

2 Likes

#5

Thank you very much! I spent whole day trying to understand, what’s wrong. Thank you! :grinning:

0 Likes

#6

Your response helped save me a lot of time troubleshooting my simple CRUD app. Thanks!

0 Likes