[英]Boolean logic that will not cooperate
我的網站遇到了一個問題,至少幾周以來我一直在努力解決這個問題,所以我希望有人能指出我哪里出錯了。 對於一些背景知識,我是一名應屆畢業生,這是我為最終項目構建的頂點項目,在我的演示文稿之前我並沒有完全獲得我想要的所有功能,現在我已經完成了 class 我正在嘗試介紹一下我覺得缺少的function。 話雖如此,我構建了一個帶有 Rails 后端和帶有引導主題的 Vue 前端的恐怖電影桶列表,我擁有它,您可以在其中將電影添加到監視列表並使其達到可以將它們添加到喜愛它的地步或討厭它列表,但我最不想看到的是,當您單擊按鈕將其添加到所述列表時,它會將其從未觀看列表中刪除。 我在后端設置了一個 boolean 但對於我的生活我無法正確地邏輯讓它從真到假我不知道我是否需要前端的東西或者什么但就像我說的我至少兩周來一直試圖解決這個問題,所以任何幫助都會很棒。
我的架構
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>
可能是您在此處的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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.