Skip to content

Instantly share code, notes, and snippets.

@ammezie
Created October 25, 2017 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ammezie/19bb96678f4c6b820832fc504b4b00c7 to your computer and use it in GitHub Desktop.
Save ammezie/19bb96678f4c6b820832fc504b4b00c7 to your computer and use it in GitHub Desktop.
// start/routes.js
Route.get('tasks/:id/edit', 'TaskController.edit') // updated this slightly
Route.patch('tasks/:id', 'TaskController.update')
// For the PATCH method, you might want to take a look at - http://dev.adonisjs.com/docs/4.0/request#_method_spoofing
// TaskController.js
async update ({ request, params, response, session }) {
// Get the task you want to update
const task = await Task.find(params.id)
// validate form input
const validation = await validate(request.all(), {
title: 'required|min:3|max:255'
})
// show error messages upon validation fail
if (validation.fails()) {
session.withErrors(validation.messages())
.flashAll()
return response.redirect('back')
}
// persist changes to database
task.title = request.input('title')
await task.save()
// Fash success message to session
session.flash({ notification: 'Task update!' })
return response.redirect('back')
}
@adegbengaagoro
Copy link

This is awesome. Thanks for the update on this

@adegbengaagoro
Copy link

adegbengaagoro commented Oct 25, 2017

Hi @ammezie,

This is what my edit form looks like
<form action="{{ 'tasks/' + task.id + '?_method=PUT' }}" method="POST">

However I keep ending up with this URL route:
http://127.0.0.1:3333/tasks/3/tasks/3?_method=PUT

Which results in getting an error:

HttpException
Route not found /tasks/3/tasks/3

What can I do to address this?

Thanks

@ammezie
Copy link
Author

ammezie commented Oct 26, 2017

Since you are using PUT, change your route to use it instead of PATCH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment