Resolve ArgumentError in TasksController # update

First, recognize that given0 and expected1 are errors that do not have the same number of arguments.

After that, I needed a conclusion, (task_params).

Then I wondered what it was and decided to take a look

889bc80b3c184bbcc434429245f79351.png

First check that it is checked in red

Looking at it, the 51st line is suspicious. The 50th line gets a specific record from the task table and assigns it to @task. And on line 51 there is an if statement saying if it is updated. Pay attention to @task here. Since update is an action that replaces the updated data, it also saves to the table through the model, so there is something you need. This is a strong parameter to prevent unintended data registration / update. Where are the strong parameters?

Check strong parameters


private
def task_params
  params.require(:task).permit(:content, :point_id).merge(user_id: current_user.id)
end

It is already prepared under private, and is also required when creating. It was necessary to pass the task_params defined here as an argument. Therefore, it was scolded if the number of arguments was not correct and caused an error.

Correct answer

def update
    @task = Task.find(params[:id])
    if @task.update(task_params)← Added here
      redirect_to root_path
    end

This was able to eliminate the error.

Recommended Posts

Resolve ArgumentError in TasksController # update