teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

誤字

2020/11/11 18:09

投稿

moST
moST

スコア14

title CHANGED
File without changes
body CHANGED
@@ -9,7 +9,7 @@
9
9
 
10
10
  paramsの結果
11
11
  ```
12
- <ActionController::Parameters {"array"=>[{"cnt"=>"51"}], "controller"=pi/combats", "action"=>"create"} permitted: false>
12
+ <ActionController::Parameters {"array"=>[{"cnt"=>"51"}], "controller"=pi/array", "action"=>"create"} permitted: false>
13
13
  ```
14
14
 
15
15
  params.fetch(:array, {})の結果
@@ -79,7 +79,7 @@
79
79
  },
80
80
  methods: {
81
81
  modelSave: async function () {
82
- const res = await axios.post('/api/array/create', {array: this.Array})
82
+ const res = await axios.post('/api/array', {array: this.Array})
83
83
  if (res.status !== 201) {
84
84
  process.exit()
85
85
  }
@@ -91,7 +91,7 @@
91
91
  })
92
92
  },
93
93
  Get: async function() {
94
- const res = await axios.get(` /api/array/index`)
94
+ const res = await axios.get(` /api/array`)
95
95
  if (res.status !== 200) { process.exit() }
96
96
  return res
97
97
  },

1

情報の最新化

2020/11/11 18:09

投稿

moST
moST

スコア14

title CHANGED
@@ -1,1 +1,1 @@
1
- Rails+Vueで、Vue側の値の変更ータスに保存たい
1
+ Vue側で送信した配列(JSON)の値をストロングパラメーターしようとするとNoMethodErrorエラーになる
body CHANGED
@@ -1,9 +1,30 @@
1
1
  RailsのAPIで、Vue側から値の変更をPOST送信するやり方がわかりません。
2
2
  GetリクエストでJson形式のデータを取ってきて配列に代入し、表示する方法はわかりました。
3
3
  Vue側での値の変更をRailsのモデルに反映するPostリクエストはどのようにすればよいでしょうか?
4
- 配列を引数にpostリクエストをすると、404エラーが帰ってきます。
5
4
  今回は、ボタンを押すと値が1つ増える仕組みで試験しています。
6
5
 
6
+ 11/12追記(タイトル変更)
7
+ byebugで確認したところ、paramsとparams.fetchの値は入っていましたが、permitでエラーになっています。
8
+ paramsとparams.fetchの値は以下の通りです。
9
+
10
+ paramsの結果
11
+ ```
12
+ <ActionController::Parameters {"array"=>[{"cnt"=>"51"}], "controller"=pi/combats", "action"=>"create"} permitted: false>
13
+ ```
14
+
15
+ params.fetch(:array, {})の結果
16
+ ```
17
+ [<ActionController::Parameters {"cnt"=>"51"} permitted: false>]
18
+
19
+ ```
20
+ params.fetch(:array, {}).permit(:cnt)の結果
21
+ ```
22
+ *** NoMethodError Exception: undefined method `permit' for #<Array:0x00007ff8350162d0>
23
+ nil
24
+ ```
25
+
26
+
27
+
7
28
  ```APIcontroller
8
29
  def index
9
30
  @cnt = Array.all.sum(:cnt)
@@ -13,14 +34,15 @@
13
34
  end
14
35
 
15
36
  def create
37
+ @test = Array.find(current_user.id)
16
- current_user.Array.create!(array_params)
38
+ @test.update(array_params)
17
39
  head :created
18
40
  end
19
41
 
20
42
  private
21
43
 
22
44
  def array_params
23
- params.require(:array).permit(:cnt)
45
+ params.fetch(:array, {}).permit(:cnt)
24
46
  end
25
47
  ```
26
48