I'm not sure why I'm getting an ActionClauseError for my PUT route

In my router I currently have the following:

resources "/customer_common_tasks", CustomerCommonTasksController

In the CustomerCommonTasksController lives the following update function:

  def update(conn, %{"id" => id, "customer_common_tasks" => customer_common_tasks_params}) do
 
    customer_common_tasks = Customer.get_customer_common_tasks!(id)
    with {:ok, %CustomerCommonTasks{} = customer_common_tasks} <- Customer.update_customer_common_tasks(customer_common_tasks, customer_common_tasks_params) do
      render(conn, "show.json", customer_common_tasks: customer_common_tasks)
    end
  end

When I send a PUT request to the endpoint I get back an ActionClauseError as if there was no route to the update function, however when I run mix phx.routes I can see it exists:

PUT     /api/customer_common_tasks/:id       myApp.CustomerCommonTasksController :update

The logger notes that my request carries the following params:

14:04:39.342 request_id=2mli8bmgvp03ormudk00008h remote_ip=ip [debug] Processing with MyApp.CustomerCommonTasksController.update/2
  Parameters: %{"customer_common_task" => %{"belongs_to" => nil, "name" => "A Task", "notes" => "some notes", "customer_id" => nil, "resolved" => true}, "id" => "13"}

But when even if I use a function with a header of (connm, _params) I can’t get the function to trigger.

I’m not sure where I have gone wrong here and any advice would be greatly appreciated

There’s a typo, you send "customer_common_task" but your action requires "customer_common_tasks".

I would recommend changing the signature from

def update(conn, %{"id" => id, "customer_common_tasks" => customer_common_tasks_params}) do

to

def update(conn, params) do

and then simply doing params.id and params.customer_common_tasks. This means you can easily extend it with validation, either by ecto or some other library, or just manually checking the properties.

By pattern matching on it directly you get confusing error messages, both in the console and what you send to the client. Just a recommendation though, there are many ways of solving a problem :slight_smile:

2 Likes

plural

singular

1 Like

Unbelievable. I’m not even going to tell you how long I spent on this.

Thanks for pointing out the obvious fellas, all fixed now!

Also, I’m not a fella.

1 Like

Apologies, in my part of the UK ‘fella’ means ‘everyone around me who is not me’, no offense intended!

And thanks for the expanded suggestion, I’m currently working from the JSON generator and plan to refactor some later so that is definitely useful.

1 Like