ga-wdi-boston / full-stack-project

Other
8 stars 64 forks source link

Post request not working #1017

Closed 3point14guy closed 7 years ago

3point14guy commented 7 years ago

Having trouble getting my POST request to work. I can't seem to capture the data input into my modal, or at least I think that is the problem...

I get a 400 error Bad request. On the rails side, I get: ActionController::ParameterMissing (param is missing or the value is empty: movie):

const addMovie = function (event) {
  event.preventDefault()
  const data = getFormFields(this)
  console.log('events addMovie')
  api.addAMovie(data)
    .then(ui.addMovieSuccess)
    .catch(ui.addMovieFailure)
}
const addAMovie = function (data) {
  console.log('api addAMovie')
  console.log(data)
  return $.ajax({
    url: config.apiOrigin + '/movies',
    method: 'POST',
    headers: {
      Authorization: 'Token token=' + store.user.token
    },
    data
  })
}
3point14guy commented 7 years ago

Ben and Chris looked over my code with me. The problem was in my HTML. I had left out the name= portion in the modal that tells what the input in the form equates to in my api.

Adding name="movie[name]": to my input type solved the problem.

<input type="text" class="form-control" name="movie[name]"
                  id="inputMovie" placeholder="Movie Title">