Method spoofing does'nt work with Route Resources

My Route resource definition:

Route.resource('users', 'UserController');

My User update method in User Controller:

async update ({ params, request, response }) {
        const user = await User.findOrFail(params.id);

        const data = request.only([
        'username',
        'email',
        'password'
        ]);

        user.merge(data);
        await user.save();

        response.redirect('/users');
    }

In my edge file, I defined my form like this:

<form method="POST" action="{{ route('users.update', {id: user.id}) }}">

Shouldn’t Adonis automatically insert “_method: PUT” into my form’s Action?
The generated HTML looks like this:

<form method="POST" action="/users/7">

Do I need to enter the verb manually in the form? How to do this?
Remembering that in config/app.js is enabled the option:

    allowMethodSpoofing: true,
1 Like

I believe the title of the thread is bit misleading. The method spoofing does work as it should. However, the route helper doesn’t append _method=PUT to the url, coz the helper doesn’t know that you are setting the form section. For example

There is no way for the route helper to know when or when not to append the _method in the following 2 examples.

<a href="">{{ route('some-route') }} </a>
<form action="{{ route('some-route') }}">

So the answer to have your own helper or append _method by hand

1 Like

I apologize for the confusion in the title.

I had understood that when using named routes and referencing them in the Edge template, Adonis automatically inserted _method into the form’s Action when the routes are named as .update and .delete (when the option is active in config / app.js ).

My solution was:

<form method="POST" action="{{ route('users.update', {id: user.id}) }}?_method=PUT">

Thank you @virk

1 Like