protocol nume(phoenix-framework)无法实现

时间:2016-05-20 10:40:55

标签: elixir phoenix-framework

我在订单控制器中添加了一个“付款”功能,并添加了一条路线。

def payment(conn, %{"id" => id}) do
  foods = Repo.all(Food)
  order =
    Repo.get!(assoc(conn.assigns[:table], :orders), id)
    |> Repo.preload(order_items: :food)
  order_item_changeset =
    order
    |> build_assoc(:order_items)
    |> Pos1.OrderItem.changeset()
  order_changeset = Order.changeset(order)

  render(conn, "payment.html", order: order, order_item_changeset: order_item_changeset, order_changeset: order_changeset, foods: foods, payment: @payment)
end

resources "/tables", TableController do
  resources "/orders", OrderController do
    get "/payment", OrderController, :payment
end

我在表格和订单下添加了付款路线,因为付款必须显示其表格编号和订单ID。我在show.html的订单模板中添加了指向付款页面的链接。但是,它会导致Protocol.UndefinedError。

[error] #PID<0.9096.0> running Pos1.Endpoint terminated
Server: localhost:4000 (http)
Request: GET /tables/1/orders/14
** (exit) an exception was raised:
    ** (Protocol.UndefinedError) protocol Enumerable not implemented for nil
        (elixir) lib/enum.ex:1: Enumerable.impl_for!/1
        (elixir) lib/enum.ex:116: Enumerable.reduce/3
        (elixir) lib/enum.ex:1486: Enum.reduce/3
        (pos1) Pos1.Router.Helpers.segments/3
        (pos1) web/router.ex:1: Pos1.Router.Helpers.table_order_order_path/5
        (pos1) web/templates/order/show.html.eex:59: Pos1.OrderView."show.html"/1
        (pos1) web/templates/layout/app.html.eex:41: Pos1.LayoutView."app.html"/1
        (phoenix) lib/phoenix/view.ex:344: Phoenix.View.render_to_iodata/3
        (phoenix) lib/phoenix/controller.ex:633: Phoenix.Controller.do_render/4
        (pos1) web/controllers/order_controller.ex:1: Pos1.OrderController.action/2
        (pos1) web/controllers/order_controller.ex:1: Pos1.OrderController.phoenix_controller_pipeline/2
        (pos1) lib/phoenix/router.ex:261: Pos1.Router.dispatch/2
        (pos1) web/router.ex:1: Pos1.Router.do_call/2
        (pos1) lib/pos1/endpoint.ex:1: Pos1.Endpoint.phoenix_pipeline/1
        (pos1) lib/plug/debugger.ex:93: Pos1.Endpoint."call (overridable 3)"/2
        (pos1) lib/phoenix/endpoint/render_errors.ex:34: Pos1.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

我该如何解决这个问题?我应该使用其他方法来定义功能支付吗?

这是来自web / templates / order / show.html.eex:59的代码

 <%= link "Confirm orders", to: table_order_order_path(@conn, :payment, @table, @order, @payment) %> 




<%= if @payment do %>
<%= link "Confirm orders", to: table_order_order_path(@conn, :payment, @table, @order, @payment) %>
<% end %>

1 个答案:

答案 0 :(得分:6)

错误Role表示您将public function users() { return $this->belongsToMany(User::class, 'user_roles'); } 传递到应使用列表或可枚举的地方。

如果向下看堆栈,您可以看到它是:protocol Enumerable not implemented for nil订单路径可能接受订单或订单ID,即数据库中的nil。您只需尝试在控制器中打印Pos1.Router.Helpers.table_order_order_path/5NULL即可查看哪个存在问题。

如果你再低一点,它会告诉你问题在于模板:order。在模板中试试这个:

order_item_changeset