Update form: Expected "intid" to be a string


#1

I am trying to update a record but am getting an error Expected "intid" to be a string on loading the edit view. How do I format the Form action?

Routes:

Route.get('/projects/:id/interviews/:intid/edit', 'InterviewController.edit').as('interviews.edit').middleware(['auth'])
Route.patch('/projects/:id/interviews/:intid', 'InterviewController.update').as('interviews.update').middleware(['auth'])

Controller:

async edit ({ params, view }) {

    const interview = await Interview
    .query()
    .with('projects')
    .where('id', '=', params.intid) // Interview Id
    .whereHas('projects', (builder) => {
      builder.where('id', params.id) // Project Id
    })
    .firstOrFail()


    return view.render('modules.interviews.edit', { interview: interview.toJSON() }
    )
  }


  async update ({ params, request, response, session }) {
     const interview = await Interview.findOrFail(interview.id)
      interview.name = request.input('editTitle')
      interview.email = request.input('editEmail')
      interview.department = request.input('editDepartment')
      interview.manager = request.input('editManager')
      interview.location = request.input('editLocation')

      await interview.save()

      session.flash({ successMessage: 'Interveiew updated' })
      return response.route('interviews.index')
  }

edit.edge form

<form action="{{ route('interviews.update', { id: interview.id } ) + '?_method=PATCH' }}" method="POST">
      {{ csrfField() }}
...

Maybe my entire approach is wrong! Many thanks in advance


#2

So what does intid return? What does console.log says?
Where do you get params. intid from / where do you post it?

And is there any reason why you write .where('id', '=', params.intid) instead of
.where('id', params.intid)?

Regarding the issue, you should also validate the data before you’re creating the model.


#3

params returns

{
  "id": "22", // project id
  "intid": "10" // interview id
}

#4

Hey @thurzo! :wave:

It seems that you never setup intid in your form’s action when you generate the route.


#5

Hi @ romain.lanz

Thanks I added that and that did the trick.

Thanks,