質問編集履歴

2

追記を削除

2018/07/26 10:31

投稿

ruby_0ct
ruby_0ct

スコア57

test CHANGED
File without changes
test CHANGED
@@ -153,85 +153,3 @@
153
153
  <% end %>
154
154
 
155
155
  ```
156
-
157
-
158
-
159
- # 追記 undefined method `build' for nil:NilClassエラー
160
-
161
- has_manyとbelongs_toが逆でしたので修正いたしましたが、buildエラーが発生しました。
162
-
163
- 解決策を頂けると助かります。
164
-
165
- ```
166
-
167
- undefined method `build' for nil:NilClass
168
-
169
- ```
170
-
171
- ## コントローラ
172
-
173
- ```ruby
174
-
175
- def new
176
-
177
- @postit = Postit.new
178
-
179
- @postit.postitcategory.build
180
-
181
- end
182
-
183
- ```
184
-
185
- ##モデル
186
-
187
- postit.rb
188
-
189
- ```ruby
190
-
191
- class Postit < ApplicationRecord
192
-
193
- belongs_to :postitcategory, optional: true
194
-
195
-
196
-
197
- accepts_nested_attributes_for :postitcategory
198
-
199
- end
200
-
201
-
202
-
203
- ```
204
-
205
- postcategory.rb
206
-
207
- ```ruby
208
-
209
- class Postitcategory < ApplicationRecord
210
-
211
- has_many :postits
212
-
213
- end
214
-
215
-
216
-
217
- ```
218
-
219
-
220
-
221
- ## View
222
-
223
- ```ruby
224
-
225
- <div class="field">
226
-
227
- <%= fields_for(:postitcategory) do |c| %>
228
-
229
- <%= c.label :category %>
230
-
231
- <%= c.text_field :category %>
232
-
233
- <% end %>
234
-
235
- </div>
236
-
237
- ```

1

エラー発生

2018/07/26 10:30

投稿

ruby_0ct
ruby_0ct

スコア57

test CHANGED
File without changes
test CHANGED
@@ -153,3 +153,85 @@
153
153
  <% end %>
154
154
 
155
155
  ```
156
+
157
+
158
+
159
+ # 追記 undefined method `build' for nil:NilClassエラー
160
+
161
+ has_manyとbelongs_toが逆でしたので修正いたしましたが、buildエラーが発生しました。
162
+
163
+ 解決策を頂けると助かります。
164
+
165
+ ```
166
+
167
+ undefined method `build' for nil:NilClass
168
+
169
+ ```
170
+
171
+ ## コントローラ
172
+
173
+ ```ruby
174
+
175
+ def new
176
+
177
+ @postit = Postit.new
178
+
179
+ @postit.postitcategory.build
180
+
181
+ end
182
+
183
+ ```
184
+
185
+ ##モデル
186
+
187
+ postit.rb
188
+
189
+ ```ruby
190
+
191
+ class Postit < ApplicationRecord
192
+
193
+ belongs_to :postitcategory, optional: true
194
+
195
+
196
+
197
+ accepts_nested_attributes_for :postitcategory
198
+
199
+ end
200
+
201
+
202
+
203
+ ```
204
+
205
+ postcategory.rb
206
+
207
+ ```ruby
208
+
209
+ class Postitcategory < ApplicationRecord
210
+
211
+ has_many :postits
212
+
213
+ end
214
+
215
+
216
+
217
+ ```
218
+
219
+
220
+
221
+ ## View
222
+
223
+ ```ruby
224
+
225
+ <div class="field">
226
+
227
+ <%= fields_for(:postitcategory) do |c| %>
228
+
229
+ <%= c.label :category %>
230
+
231
+ <%= c.text_field :category %>
232
+
233
+ <% end %>
234
+
235
+ </div>
236
+
237
+ ```