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

回答編集履歴

2

追記

2020/09/12 15:04

投稿

unhappychoice
unhappychoice

スコア1531

answer CHANGED
@@ -1,4 +1,69 @@
1
1
  > Unpermitted parameters: :utf8, :authenticity_token, :pokemon, :exp_gender, :exp_item, :party_name, :commit
2
2
 
3
3
  とあるので、コントローラーにて strong parameter の設定を確認してください。
4
- リクエストのパラメーターには含まれていても、保存時に許可されていないように見えます。
4
+ リクエストのパラメーターには含まれていても、保存時に許可されていないように見えます。
5
+
6
+ ### 追記
7
+
8
+ ```
9
+ <!-- = f.fields_for :pokemon do |pokemon| -->
10
+ = f.fields_for :pokemons do |pokemon| <!-- accept_nested_attributes_for の名称と合わせる必要があります -->
11
+ ```
12
+
13
+ ```
14
+ class PartiesController < ApplicationController
15
+ # ...
16
+
17
+ def create
18
+ # accept_nested_attributes_for しているので、 @party.save で Pokemon も保存されます
19
+ # @pokemon = Pokemon.new(pokemon_params)
20
+ # @pokemon.save
21
+ @party = Party.new(party_params)
22
+ @party.save
23
+ redirect_to root_path, notice: 'パーティを作成しました'
24
+ end
25
+
26
+ def show
27
+ @party = Party.find(params[:id])
28
+ end
29
+
30
+ def edit
31
+ @party = Party.find(params[:id])
32
+ end
33
+
34
+ def update
35
+ @party = Party.find(params[:id])
36
+ end
37
+
38
+ def destroy
39
+ @party = Party.find(params[:id])
40
+ end
41
+
42
+ private
43
+
44
+ # 不要なはず
45
+ #def pokemon_params
46
+ # params.permit(
47
+ # :name, :nickname, :gender, :ability, :nature, :item, :move1, :move2, :move3, :move4,
48
+ # :cs_hp, :cs_attack, :cs_defense, :cs_special_attack, :cs_special_defense, :cs_speed,
49
+ # :bs_hp, :bs_attack, :bs_defense, :bs_special_attack, :bs_special_defense, :bs_speed,
50
+ # :ev_hp, :ev_attack, :ev_defense, :ev_special_attack, :ev_special_defense, :ev_speed
51
+ # )
52
+ #end
53
+
54
+ def party_params
55
+ # params は params[:party][:pokemons_attributes] のように入っているので require(:party) は必要そうです
56
+ params.require(:party).permit(
57
+ :party_name,
58
+ pokemons_attributes:[
59
+ :name, :nickname, :gender, :ability, :nature, :item, :move1, :move2, :move3, :move4,
60
+ :cs_hp, :cs_attack, :cs_defense, :cs_special_attack, :cs_special_defense, :cs_speed,
61
+ :bs_hp, :bs_attack, :bs_defense, :bs_special_attack, :bs_special_defense, :bs_speed,
62
+ :ev_hp, :ev_attack, :ev_defense, :ev_special_attack, :ev_special_defense, :ev_speed
63
+ ]
64
+ )
65
+ end
66
+ end
67
+ ```
68
+
69
+ Pokemon のパラメーターすべてを確認していないのと、 View が中略されているので確実に動くかはわかりませんが、雰囲気はこのような形だと思います。

1

修正

2020/09/12 15:04

投稿

unhappychoice
unhappychoice

スコア1531

answer CHANGED
@@ -1,3 +1,4 @@
1
1
  > Unpermitted parameters: :utf8, :authenticity_token, :pokemon, :exp_gender, :exp_item, :party_name, :commit
2
2
 
3
- とあるので、コントローラーにて strong parameter の設定を確認してください。
3
+ とあるので、コントローラーにて strong parameter の設定を確認してください。
4
+ リクエストのパラメーターには含まれていても、保存時に許可されていないように見えます。