1
votes

I have orders and order_items controllers and models generated automatically.

My router looks like this:
resources "/orders", OrderController do
  resources "/order_items", OrderItemController
end

On the order show.html page I'm calling different order_item actions. I can create and delete order_item associating it with order:

<%= render Pos1.OrderItemView, "item_quantity.html", changeset: @order_item_changeset, action: order_order_item_path(@conn, :create, @order) %>
<%= link "delete", to: order_order_item_path(@conn, :delete, @order, @order_item), method: :delete %>

However if i try to call update action (not from edit.html of order_item, but show.html of order, like create/delete above) it returns following error:

no route found for POST /orders/53/order_items/62 (Pos1.Router)

Well, I understand that the method should be PUT (for update), but if I use link to use method: "put"

<%= link "+", to: order_order_item_path(@conn, :update, @order, @order_item), method: "put" %>

Still can't make it work. It seems weird to me that I can call create and delete, but not update action from the show.html of the order. What's more, I can call update from order_item edit.html page and it works perfectly. What am i doing wrong? Have been struggling for a while with this.

Thanks in advance!

Edit: My update action from order_item:

def update(conn, %{"id" => id}) do
order_item = Repo.get!(OrderItem, id)
order = Repo.get!(Order, order_item.order_id)
changeset = OrderItem.changeset(order_item, %{quantity: order_item.quantity + 1})

case Repo.update(changeset) do
  {:ok, order_item} ->

    conn
    |> put_flash(:info, "Order item quantity increased successfully.")
    |> redirect(to: table_order_path(conn, :show, order.table, order))
  {:error, _changeset} ->
    conn
    |> put_flash(:error, "Failed to increase order item!")
    |> redirect(to: table_order_path(conn, :show, order.table, order))
end

end

Error: enter image description here

Order_item.html.eex:

<tr>
  <td><%= @order_item.food.name %></td>
  <td><%= @order_item.quantity %></td> 
  <td><%= link "+", to: order_order_item_path(@conn, :update, @order_item), method: :put %></td>
  <td><%= link "-", to: order_order_item_path(@conn, :delete, @order, @order_item), method: :delete, class: "btn btn-danger btn-xs" %> </td>
</tr>
1
What's the error when you do method: "put"? Also note that there's a syntax error in that code (maybe you changed before posting here?). - Dogbert
Hey @Dogbert, Sorry, I fixed the syntax. As for the error, I get "No helper clause for Pos1.Router.Helpers.order_order_item_path defined for action :update with arity 3. Please check that the function, arity and action are correct." - Ilya
Ah, try method: :patch instead. - Dogbert
With method: :patch unfortunately get same error as with method: :put - Ilya
Can you post the stacktrace for that error? You're not calling order_order_item_path with :update with a total of 3 arguments anywhere in the code you've posted. Also, try searching your codebase for a call to that function with 3 arguments. - Dogbert

1 Answers

1
votes

You're missing an argument in the call to order_order_item_path.

This:

<td><%= link "+", to: order_order_item_path(@conn, :update, @order_item), method: :put %></td>

should be:

<td><%= link "+", to: order_order_item_path(@conn, :update, @order, @order_item), method: :put %></td>