My Desire for Reality
I am looking to update multiple related values in Rails by sending an update request from JavaScript. While creating data was seamless, I encountered difficulties when attempting to update it.
#Code
JavaScript*
export const actions = {
// Posting the selected book
post (context) {
const list = context.state.todos.list
const selectedBook = context.state.book.selectedBook
// Mapping out array
const postItemsAttributes =
list.map((item) => {
return {
content: item.content,
status: item.status
}
})
// plugin/bookInfo $title,$author,$image
this.$axios.$post(url.POST_API + 'posts', {
post: {
title: this.$title(selectedBook),
author: this.$author(selectedBook),
image: this.$image(selectedBook),
post_items_attributes: postItemsAttributes
}
})
.then((responseBook) => {
context.commit('book/userBook', responseBook)
context.commit('book/clearBook')
context.commit('todos/clear')
})
},
//////////////////////////////////////////////////////////////////////////////////
// Issue with updating values
update (context) {
const list = context.state.todos.list
const bookId = context.state.book.selectedBook.id
const content =
list.map((item) => {
return {
content: item.content,
status: false
}
})
this.$axios.$patch(url.POST_API + 'posts/' + bookId, {
post: {
post_items_attributes: content
}
})
}
//////////////////////////////////////////////////////////////////////////////////
}
Rails controller
class Api::V1::PostsController < ApplicationController
def create
posts = Post.new(post_params)
if posts.save
render json: "OK", status: 200
else
render json: "EEEOR", status: 500
end
end
def update
post = Post.find(params[:id])
post.post_items.update(content_params)
end
private
# update
def content_params
params.require(:post).permit(post_items_attributes:[:id, :content, :status])
end
#create
def post_params
params.require(:post).permit(:title, :author, :image, post_items_attributes: [:id, :content, :status])
end
end
model/post
class Post < ApplicationRecord
has_many :post_items, dependent: :destroy
accepts_nested_attributes_for :post_items, allow_destroy: true
validates :title, presence: true
validates :author, presence: true
validates :image, presence: true
end
model/post_item
class PostItem < ApplicationRecord
belongs_to :post
end
Error
api_1 | Started PATCH "/api/v1/posts/16" for 172.29.0.1 at 2021-09-06 22:15:10 +0900
api_1 | Processing by Api::V1::PostsController#update as HTML
api_1 | Parameters: {"post"=>{"post_items_attributes"=>[{"content"=>"Test", "status"=>false}]}, "id"=>"16"}
api_1 | Post Load (13.0ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT $2 [["id", 16], ["LIMIT", 1]]
api_1 | ↳ app/controllers/api/v1/posts_controller.rb:23:in `update'
api_1 | PostItem Load (15.4ms) SELECT "post_items".* FROM "post_items" WHERE "post_items"."post_id" = $1 [["post_id", 16]]
api_1 | ↳ app/controllers/api/v1/posts_controller.rb:24:in `update'
api_1 | Completed 204 No Content in 49ms (ActiveRecord: 29.5ms | Allocations: 1576)
What I attempted on my own
① I tried using post_all, but it didn't work because post_all is used directly in the model.