我目前正在通過“Post_tags”在“Post”和“tags”之間創建多對多關系。我想要的是能夠將 4 件事(標題、內容、標簽、user_id)保存到我的后端,以便我可以顯示或更新帖子。目前,我可以保存新帖子并更新沒有標簽的帖子。我目前的模型看起來像這樣:
后模型
class Post < ApplicationRecord
belongs_to :user
has_many :post_tags
has_many :tags, through: :post_tags
end
Post_tag 模型
class PostTag < ApplicationRecord
belongs_to :post
belongs_to :tag
end
標簽 模型
class Tag < ApplicationRecord
has_many :post_tags
has_many :posts, through: :post_tags
end
我正在使用 React 前端添加帶有獲取請求的新帖子
export default function Post({currentUser}){
const[title, setTitle] = useState("");
const[content, setContent] = useState("");
const[tags, setTags] = useState("");
const user_id = currentUser.id
const navigate = useNavigate();
function handleSubmit(e){
e.preventDefault();
const newPost = {
title,
content,
user_id
}
fetch(`/post`, {
method: 'POST',
headers: {"Content-Type": 'application/json'},
body: JSON.stringify(newPost),
}).then((r) =>{
if (r.ok){
r.json().then(navigate('/profile'))
alert("New post created!")
}else{
alert("New post creation failed")
}
})
}
return(
<div className="post-form-container">
<form className="post-form" onSubmit={handleSubmit}>
<label>Title</label><br/>
<input
className='title-input'
type='text'
onChange={(e) => setTitle(e.target.value)}
value={title}>
</input><br/>
<label>Content</label><br/>
<textarea
className="content-input"
type='text'
onChange={(e) => setContent(e.target.value)}
value={content}
placeholder="Start typing~">
</textarea><br/>
<label>Tags: seperated by commas</label><br/>
<input
className="tags-input"
type='text'
onChange={(e) => setTags(e.target.value)}
value={tags}>
</input><br/>
<button className="post-btn" type="submit">Submit</button>
</form>
</div>
最后,我的架構檔案是這樣的:
ActiveRecord::Schema[7.0].define(version: 2022_06_07_003341) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "post_tags", force: :cascade do |t|
t.integer "post_id"
t.integer "tag_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "posts", force: :cascade do |t|
t.string "title"
t.string "content"
t.string "tags"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "tags", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "username"
t.string "email"
t.string "password_digest"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
我的問題是我應該如何將“標簽”輸入從前端發送到后端以保存在我的情況下?由于標簽在單獨的表中,我是否需要為標簽創建另一個 POST 請求?我是 Rails 的初學者,請幫忙。
uj5u.com熱心網友回復:
添加accepts_nested_attributes_for :tags到您的Post模型
class Post < ApplicationRecord
has_many :post_tags
has_many :tags, through: :post_tags
accepts_nested_attributes_for :tags
end
和tags_attributes: tags.split(',').map((el) => { return {name: el} })你的newPost變數
const newPost = {
title,
content,
user_id,
tags_attributes: tags.split(',').map((el) => { return {name: el} })
}
您還需要在控制器中添加tags_attributes: %i[name]強大的引數Post
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/489660.html
