как удалить запись в rails API и vue Js - PullRequest
0 голосов
/ 19 февраля 2019

Я пытаюсь удалить API рельсов ActiveRecord и Vue js, используя библиотеку axios.Я могу получать и добавлять записи без каких-либо проблем. У меня есть проблема с удалением записи

Вот мой код

rails API .....

# DELETE v1/customers/1
 def destroy
  @customer = Customer.find(params[:id])
  @customer.destroy
end

Vue Js ........

<tr v-for="customer in customers" :key="customer.id">
  <td>{{customer.first_name}}</td>
   <td>{{customer.last_name}}</td>
    <td>{{customer.email}}</td>
     <td>{{customer.id}}</td>
     <td><button @click="removecustomer(customer.id)"
     type="button" class="btn btn-outline-danger btn-sm">Delete</button></td>

 export default {
  name: 'customers',
    data (){
        return{
            customers:[]
        }
    },
    created (){
        this.getallcustomers()
    },
     methods: {
       getallcustomers () {
             let uri = 'http://localhost:3000/v1/customers';
            this.axios.get(uri).then((response) => {
               this.customers = response.data;
               console.log(this.customers);

            });
       },
        removecustomer(id){
             let uri = `http://localhost:3000/v1/customers/${customer.id}`;
             this.axios.delete(uri).then((res) => {
               this.customers = this.customers.filter(customer => customer.id !== id);



            });
        }
    }

}

Так что мои removecustomer методы выдают ошибку customer is not defined"

Мне нужна помощь

1 Ответ

0 голосов
/ 19 февраля 2019

В функцию removecustomer передается только идентификатор клиента в качестве параметра (id), а не весь объект customer, поэтому вы получаете неопределенную ошибку.

Заменить:

let uri = `http://localhost:3000/v1/customers/${customer.id}`

С:

let uri = `http://localhost:3000/v1/customers/` + id
...