質問編集履歴

2

改善

2019/06/24 13:22

投稿

m.shinji
m.shinji

スコア20

test CHANGED
File without changes
test CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  railsでcocoonというgemを使い一つのフォームから親子孫を一気に保存できるようにしたいのですが、孫の部分で「Unpermitted parameter :grandchildren」が出て、保存に失敗してしまいます。
4
4
 
5
+ これを保存できるようにしたいです。
6
+
5
7
  ### 起きてるエラー
6
8
 
7
9
  ```
@@ -107,3 +109,93 @@
107
109
  end
108
110
 
109
111
  ```
112
+
113
+ ### View
114
+
115
+ ```new
116
+
117
+ <%= form_for(@parent, url:{controller:'parents', action:'create'})do |f| %>
118
+
119
+     <div class="field">
120
+
121
+ <%= f.label :名前%>
122
+
123
+ <%= f.text_field :name, class: 'form-control' %>
124
+
125
+ </div>
126
+
127
+
128
+
129
+     <div class="field">
130
+
131
+ <%= f.label :説明%>
132
+
133
+ <%= f.text_field :description, class: 'form-control' %>
134
+
135
+ </div>
136
+
137
+     <div id="children">
138
+
139
+
140
+
141
+ <%= f.fields_for :children do |i| %>
142
+
143
+ <%= render "child_fields", f: i %>
144
+
145
+ <% end %>
146
+
147
+
148
+
149
+ </div>
150
+
151
+ <% end %>
152
+
153
+ ```
154
+
155
+ _child_fields.html.erb
156
+
157
+ ```
158
+
159
+     <div class="field">
160
+
161
+ <%= f.label :名前%>
162
+
163
+ <%= f.text_field :name, class: 'form-control' %>
164
+
165
+ </div>
166
+
167
+
168
+
169
+     <div class="field">
170
+
171
+ <%= f.label :説明%>
172
+
173
+ <%= f.text_field :description, class: 'form-control' %>
174
+
175
+ </div>
176
+
177
+     <div id="children">
178
+
179
+
180
+
181
+ <%= f.fields_for :grandchildren do |ii| %>
182
+
183
+ <%= render "grand_child_fields", f: ii %>
184
+
185
+ <% end %>
186
+
187
+ ```
188
+
189
+ _grand_child_fields.html.erb
190
+
191
+ ```
192
+
193
+     <div class="field">
194
+
195
+ <%= f.label :名前%>
196
+
197
+ <%= f.text_field :name, class: 'form-control' %>
198
+
199
+ </div>
200
+
201
+ ```

1

誤字

2019/06/24 13:22

投稿

m.shinji
m.shinji

スコア20

test CHANGED
File without changes
test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  ```
8
8
 
9
- Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxx", "parent"=>{"name"=>"a", "overview"=>"a", "children_attributes"=>{"0"=>{"name"=>"aa", "description"=>"aa", "grandchildren"=>{"name"=>"aaa"}, "_destroy"=>"false"}}}, "commit"=>"投稿する"}
9
+ Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxx", "parent"=>{"name"=>"a", "description"=>"a", "children_attributes"=>{"0"=>{"name"=>"aa", "description"=>"aa", "grandchildren"=>{"name"=>"aaa"}, "_destroy"=>"false"}}}, "commit"=>"投稿する"}
10
10
 
11
11
  Unpermitted parameter: :grandchildren
12
12
 
@@ -72,19 +72,15 @@
72
72
 
73
73
 
74
74
 
75
- def create
75
+  def create
76
76
 
77
- @submit = Submit.new(submit_params)
77
+ @parent = Parent.new(parent_params)
78
-
79
- @submit.user_id = current_user.id
80
-
81
- @submit.purposes = Purpose.where(id: params[:submit][:purposes])
82
78
 
83
79
  #新しい投稿の保存に成功した場合
84
80
 
85
- if @submit.save
81
+ if @parent.save
86
82
 
87
- redirect_to submit_path(@submit)
83
+ redirect_to parent_path(@parent)
88
84
 
89
85
  #新しい投稿の保存に失敗した場合
90
86
 
@@ -104,7 +100,7 @@
104
100
 
105
101
  def parent_params
106
102
 
107
- params.require(:parent).permit(:name, :old, children_attributes: [:name,:old, grandchild_attributes: %(name)])
103
+ params.require(:parent).permit(:name, description, children_attributes: [:id, :name, description, :_destroy, grandchildren_attributes: [:id, :name, :_destroy]])
108
104
 
109
105
  end
110
106