【发布时间】:2021-08-31 04:32:11
【问题描述】:
我想要实现的目标
我正在创建一个应用程序。视图是 Nuxt,服务器是 Rails。 我无法随意注册文本。
我正在注册图书信息。 我要注册的信息有多个,post_items_attributes的内容和状态有多个,但是只能注册一个。在这种情况下我该怎么办? 如果设置为0,只能注册一个,不知道怎么办。
this.$axios.$post(url.POST_API + 'posts', {
post: {
title: this.$title(selectedBook),
author: this.$author(selectedBook),
image: this.$image(selectedBook),
post_items_attributes: [{
/////////////////////////////////////////////////
content: list[0].content,
status: list[0].status
////////////////////////////////////////////////
}]
}
})
.then((responseBook) => {
context.commit('book/userBook', responseBook)
context.commit('book/clearBook')
context.commit('todos/clear')
})
},
this.$store.state.todos.list
export const state = () => ({
list: []
})
在列表中
代码
Nuxt
const list = context.state.todos.list
const selectedBook = context.state.book.selectedBook
// plugin/bookInfo $title,$author,$image
this.$axios.$post(url.POST_API + 'posts', {
// send to Rails
post: {
title: this.$title(selectedBook),
author: this.$author(selectedBook),
image: this.$image(selectedBook),
post_items_attributes: [{
/////////////////////////////////////////////////////////////
content: list[0].content,
status: list[0].status
////////////////////////////////////////////////////////////
}]
}
})
.then((responseBook) => {
context.commit('book/userBook', responseBook)
context.commit('book/clearBook')
context.commit('todos/clear')
})
},
导轨
def create
posts = Post.new(post_params)
if posts.save
render json: 'OK', status: 200
else
render json: 'EEEOR', status: 500
end
end
private
def post_params
params.require(:post).permit(:title, :author, :image, post_items_attributes: [:id, :content, :status])
end
【问题讨论】:
标签: javascript ruby-on-rails vue.js nuxt.js