我的网站遇到了一个问题,至少几周来我一直在努力解决这个问题,所以我希望有人能够指出我哪里出了问题。对于一些背景知识,我是一名应届毕业生,这是我为最终项目构建的顶点项目,在演示之前我没有完全获得我想要的所有功能,现在我已经完成了我正在尝试的课程介绍一下我觉得缺少的功能。话虽这么说,我用 Rails 后端和带有 Bootstrap 主题的 Vue 前端构建了一个恐怖电影愿望清单,我可以将它添加到观看列表中,并且可以将它们添加到喜爱的列表中或者讨厌它的列表,但我最不想要的事情是,当您单击按钮将其添加到所述列表时,它会将其从未观看的列表中删除。我在后端设置了一个布尔值,但对于我的生活,我无法得到正确的逻辑来让它从 true 翻转到 false 我不知道我是否需要前端的东西或什么,但就像我说的我我们已经尝试解决这个问题至少两周了,所以任何帮助都会很棒。
我的架构
create_table "hatedits", force: :cascade do |t| t.integer "movie_id" t.integer "user_id" t.string "box_art" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end create_table "lists", force: :cascade do |t| t.integer "user_id" t.integer "movie_id" t.boolean "watched" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end create_table "lovedits", force: :cascade do |t| t.integer "movie_id" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.integer "user_id" t.string "box_art" end create_table "movies", force: :cascade do |t| t.string "name" t.string "description" t.string "box_art" t.string "sub_genre" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.integer "year" t.string "category" end create_table "users", force: :cascade do |t| t.string "name" t.string "email" t.string "password_digest" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end end
我的 Rails 更新和创建函数(我认为这就是问题所在,但我只是看不到它。)
def create list = List.create( user_id: current_user.id, movie_id: params[:movie_id], watched: false, ) if list.save render json: list else render json: {errors: list.errors.full_messages}, status: 406 end end def update list = List.find_by(id: params[:id]) list.movie_id = params[:movie_id] || list.movie_id if watched = true render json: list end if list.save render json: list else render json: {errors: list.errors.full_messages}, status: 406 end end
我的前端带有移动到不同列表的按钮和方法,这些按钮和方法可以工作,但当电影移动到喜欢或讨厌的列表时,不会将电影从未观看的列表中删除,
<template> <div class="list"> <br /> <br /> <br /> <br /> <br /> <section id="portfolio" class="portfolio"> <div class="container"> <div class="row portfolio-container"> <div class="col-lg-4 col-md-6 portfolio-item filter-app" v-for="list in lists" v-bind:key="list.id"> <div class="portfolio-wrap"> <img :src="`${list.movie.box_art}`" /> <br /> <div class="portfolio-info"> <div class="portfolio-links"> <button v-on:click="lovedIt(list)">Loved It</button> <br /> <br /> <button v-on:click="hatedIt(list)">Hated It</button> </div> </div> </div> </div> </div> </div> </section> </div> </template> <style> .list { text-align: center; color: white; } img { height: 624px; width: 370px; } </style> <script> import axios from "axios"; export default { data: function () { return { list: {}, lists: {}, movie: [], currentUser: localStorage.getItem("user_id"), }; }, created: function () { this.indexLists(); }, methods: { indexLists: function () { axios.get("/lists").then((response) => { this.lists = response.data; console.log("list", response.data); }); }, addMovie: function () { axios.post("/lists", this.movie).then(() => { this.$router.push("/lists"); }); }, lovedIt: function (list) { this.list = list; axios .post("/lovedits", { user_id: this.currentUser.id, movie_id: this.list.movie_id, }) .then(() => { console.log("yo"); this.$router.push("/lovedit"); location.reload(); }); }, hatedIt: function (list) { this.list = list; axios .post("/hatedits", { user_id: this.currentUser.id, movie_id: this.list.movie_id, }) .then(() => { console.log("sup"); this.$router.push("/hatedit"); location.reload(); }); }, }, }; </script>
P粉5365327812024-04-03 10:10:21
您是否在此处的 if
语句中进行赋值而不是比较?
def update list = List.find_by(id: params[:id]) list.movie_id = params[:movie_id] || list.movie_id if watched **==** true render json: list end