前提
NuxtからRailsでサービスを作っています。当初はオブジェクト形式でRails側に値を送っていたので問題にはならなかったのですが、大量のデータを送る必要が出てきたのでJSON形式に変換して配列に格納してからRailsに送ることにしました。しかし、送ったデータがNullになってしまい登録できません。
実現したいこと
Nuxt.jsから送ったJSONデータをRails側で保存できるようにしたい。
発生している問題・エラーメッセージ
NoMethodError (undefined method `permit' for #<Array:0x000055d947d58790>):
該当のソースコード
components
js
1 addEvent () { 2 if(this.$refs.date_form.validate()) { 3 const selectDate = this.selectDate 4 const selectDateIdMap = {1: 5, 2: 250} 5 let arrayEventJson =[] 6 7 for (let i=0; i < selectDateIdMap[selectDate]; i++) { 8 const date = moment(`${this.date}`).add(i, 'd').format('YYYY-MM-DD') 9 const dateTimeStart = `${date} ${this.timePickerStart}` 10 const dateTimeEnd = `${date} ${this.timePickerEnd}` 11 const data = { 12 name: this.eventName, 13 color: this.selectedColor, 14 start: Date.parse(dateTimeStart), 15 end: Date.parse(dateTimeEnd), 16 timed: true, 17 long_time: true 18 } 19 // JSONに変換して配列に格納 20 arrayEventJson.push(JSON.stringify(data)) 21 22 } 23//配列をRails側に送る 24 this.$store.dispatch('schedule/addEvent', arrayEventJson) 25 26 } 27 28 },
store
js
1// Rails側に送信 2addEvent ({ commit }, selectedEvent) { 3 this.$axios.$post(url.SCHEDULE_API, {post: selectedEvent}, { 4 headers: { 5 'Content-Type': 'application/json' 6 } 7 }) 8// Rails側の値をstateに格納 9 .then((response) => { 10 console.log(response) 11 let data = { 12 id: response.id, 13 name: response.name, 14 color: response.color, 15 start: response.start, 16 end: response.end, 17 updated_at: response.created_at, 18 timed: response.timed, 19 long_time: response.long_time 20 } 21 commit('setEvent', data)
Rails
rb
1class Api::V1::SchedulesController < ApplicationController 2 3 def create 4 schedule = Schedule.new(post_params) 5 if schedule.save 6 render json: schedule, status: :created 7 else 8 render json: schedule, status: :internal_server_error 9 end 10 end 11 12 private 13 14 def post_params 15 params.require(:post).permit([:id, :name, :start, :end, :color, :timed, :long_time]) 16 end 17end 18
試したこと
・axiosの第3引数にheadersを追加してJSON形式でRails側に送ってもオブジェクトに変換してくれるようにしました。
あなたの回答
tips
プレビュー