Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在rails API和vue Js中删除记录_Ruby On Rails_Vuejs2_Rails Api - Fatal编程技术网

Ruby on rails 如何在rails API和vue Js中删除记录

Ruby on rails 如何在rails API和vue Js中删除记录,ruby-on-rails,vuejs2,rails-api,Ruby On Rails,Vuejs2,Rails Api,我正在尝试使用axios库删除ActiveRecord表单rails API和Vue js。我可以毫无问题地获取和添加记录。我遇到的问题是删除记录 这是我的密码 rails API Vue Js 因此,我的removecustomer方法会产生一个错误customer未定义“ 我需要帮助您只将客户id作为参数(id)传递给removecustomer函数,而不是整个customer对象,因此,您得到了一个未定义的错误 替换: let uri = `http://localhost:3000/

我正在尝试使用axios库删除ActiveRecord表单rails API和Vue js。我可以毫无问题地获取和添加记录。我遇到的问题是删除记录

这是我的密码

rails API

Vue Js

因此,我的
removecustomer
方法会产生一个错误
customer未定义“


我需要帮助

您只将客户id作为参数(
id
)传递给
removecustomer
函数,而不是整个
customer
对象,因此,您得到了一个未定义的错误

替换:

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


我将customer.id更改为id。但代码不起作用,我使用rails作为后端,因此在对象customer中只有customer.id。没有id键根据您的问题,您在JS
removecustomer
函数中出错:
customer undefined
并替换我建议的代码应该可以解决此问题。如果如果遇到任何其他困难,您可以使用您得到的适当错误更新问题,以便我能够更好地理解并帮助您。谢谢,我必须将cors配置为允许删除请求。@pkimtani是否有基于
id
以外的内容进行删除的方法,例如基于
name
?(询问)
<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);



            });
        }
    }

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