Response.redirect('back') when "back" ain't accessible with get method


#1

Hi,

I have 3 controllers:
A post on B
B post on C

My route file:

Route.get('/', 'AController.index')
Route.post('/B', 'BController.index')
Route.post('/C', 'CController.index')

When I validate the request from B to C, I do this:

const validation = await validate(data, rules)
await validate(data, rules)

if (validation.fails()) {
    session
        .withErrors(validation.messages())
        .flashExcept()

    return response.redirect('back')
}

So when the validation fails it tries to come back to B though it tells me that the route is not found:
“Route not found /B”

I’m not sure how I should handle this.


#2

You got a route not found error because the route was actually not found :smile:. With what you have, return response.redirect('back') will redirect back to /B (GET request) which is not available. You have to define the route, probably:

Route.get('/B', 'BController.index')

Note the bolded


#3

Yeah I know it’s because the route is not defined but I cannot define it, B shouldn’t be accessible with GET request.
What I don’t get is how I should handle validation in that case.


#4

I don’t get. Maybe I should ask what you are trying to achieve.


#5

Well I just have a controller A where the user enters inputs, then it goes to a second controller B where the user enters more inputs, then it goes to the controller C where he gets the summary of the actions he just did.
Does that make sense?


#6

You can save inputs from subsequent stages in session


#7

I’m not sure what you mean, I save inputs in session but what do I need to change in my code?
Where do you want me to use the inputs stored in session?


#8

In controller C I guess. Since that’s where you want to display the summary. You know to show a form, you need to make a GET request to a certain method in a controller right?


#9

That doesn’t fix my issue of validating the inputs sent from B to C though.
Well this route Route.post('/B', 'BController.index') makes me able to show a form in the index method without having to define a get route.


#10

Maybe I should allow the get request on it and just check if I have the input from A in the session/request and if not I can redirect on A.