Mr H
Mr H

Reputation: 5304

How to trigger update action in Phoenix Controller?

Under the update test I use:

%{resp_body: body} = conn
|> sign_in
|> patch(asset_path(conn, :update), Poison.encode!(payload))

I get this error:

 ** (ArgumentError) No action :update for helper Web.Router.Helpers.asset_path/2. The following actions/clauses are supported:

     asset_path(conn_or_endpoint, :create, , opts \\ [])
     asset_path(conn_or_endpoint, :delete, id, opts \\ [])
     asset_path(conn_or_endpoint, :edit, id, opts \\ [])
     asset_path(conn_or_endpoint, :index, , opts \\ [])
     asset_path(conn_or_endpoint, :new, , opts \\ [])
     asset_path(conn_or_endpoint, :show, id, opts \\ [])
     asset_path(conn_or_endpoint, :update, id, opts \\ [])
 code: |> post(asset_path(conn, :update), Poison.encode!(payload))
 stacktrace:
   (phoenix) lib/phoenix/router/helpers.ex:299: Phoenix.Router.Helpers.raise_route_error/5
   test/controllers/asset_controller_test.exs:167: (test)

When I use Postman to test it directly it works fine though.

I have used post(asset_path(conn, :create), Poison.encode!(payload)) in the insert action and it works fine.

Here is the snapshot of postman to show that code works fine, test fails.

enter image description here

Upvotes: 0

Views: 453

Answers (2)

script
script

Reputation: 2167

You can check by typing mix phx.routes. The update action always require id to work. so pass the id and it should work

Upvotes: 0

Dogbert
Dogbert

Reputation: 222198

The update route and therefore asset_path for update action requires the id of the resource. You need to pass that as the third argument of asset_path, e.g.

|> patch(asset_path(conn, :update, id), Poison.encode!(payload))

id should be the id of the record you're trying to update

Upvotes: 2

Related Questions