My Desire for Reality
I am hoping to successfully store the data transmitted from Nuxt.js in Rails. Rails is attempting to maintain data association using accepts_nested_attributes_for.
Code
Rails
model/post.rb
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.rb
class PostItem < ApplicationRecord
belong_to :post
end
app/controller/posts_controller
class Api::V1::PostsController < ApplicationController
def index
posts = Post.all
render json: posts
end
def create
post = Post.new(post_params)
if post.save
render json: 'Succeeded', status: 200
else
render json: 'Error', status: 500
end
end
private
def post_params
params.require(:post).permit(:title, :author, :image, post_items_attributes: [:content, :status])
end
end
Nuxt.js
store/todos.js
export const state = () => ({
list: [],
hidden: false
})
export const mutations = {
add (state, text) {
state.list.push({
text,
status: false
})
},
remove (state, todo) {
state.list.splice(state.list.indexOf(todo), 1)
},
edit (state, { todo, text }) {
state.list.splice(state.list.indexOf(todo), 1, { text })
},
toggle (state, todo) {
todo.status = !todo.status
},
cancel (state, todo) {
todo.status = false
},
switching (state) {
state.hidden = !state.hidden
}
}
// Rails send
export const actions = {
postTextAdd ({ commit }, content) {
const postAxios = this.$axios.$post
const url = '/api/v1/'
postAxios(url + 'posts', {
post: {
post_items_attributes: {
content: 'test',
status: false
}
}
})
.then((response) => {
commit('add', response)
console.log(response)
})
}
}
template
・・・
methods: {
addTodo () {
// postTextAddでRailsに送る
if (this.selectedTodo.status === false) {
this.$store.dispatch('todos/postTextAdd', this.itemText)
this.itemText = ''
console.log(this.todos)
} else {
this.$store.commit('todos/edit', { todo: this.selectedTodo, text: this.itemText })
this.itemText = ''
this.$store.commit('todos/toggle', this.selectedTodo)
}
},
Error
Terminal
api_1 | Started POST "/api/v1/posts" for 192.168.96.1 at 2021-08-06 07:41:38 +0900
api_1 | Processing by Api::V1::PostsController#create as HTML
api_1 | Parameters: {"post"=>{"post_items_attributes"=>{"content"=>"test", "status"=>false}}}
api_1 | Completed 500 Internal Server Error in 27ms (ActiveRecord: 0.0ms | Allocations: 4188)
api_1 |
api_1 |
api_1 |
api_1 | TypeError (no implicit conversion of Symbol into Integer):
api_1 |
api_1 | app/controllers/api/v1/posts_controller.rb:8:in `create'
What I attempted on my own
①
api_1 | app/controllers/api/v1/posts_controller.rb:8:in `create'
def create
post = Post.new(post_params)
if post.save
render json: 'Succeeded', status: 200
else
render json: 'Error', status: 500
end
end
I investigated the above, but I couldn't determine if it was related to the error because create merely saves it.
②
TypeError (no implicit conversion of Symbol into Integer):
I suspect that the data sent from Nuxt and the data received by Rails are different, however when I referenced the Rails documentation, I found no information other than symbols.