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

質問編集履歴

2

追記を削除

2018/07/26 10:31

投稿

ruby_0ct
ruby_0ct

スコア57

title CHANGED
File without changes
body CHANGED
@@ -75,45 +75,4 @@
75
75
  <%= form.submit %>
76
76
  </div>
77
77
  <% end %>
78
- ```
79
-
80
- # 追記 undefined method `build' for nil:NilClassエラー
81
- has_manyとbelongs_toが逆でしたので修正いたしましたが、buildエラーが発生しました。
82
- 解決策を頂けると助かります。
83
- ```
84
- undefined method `build' for nil:NilClass
85
- ```
86
- ## コントローラ
87
- ```ruby
88
- def new
89
- @postit = Postit.new
90
- @postit.postitcategory.build
91
- end
92
- ```
93
- ##モデル
94
- postit.rb
95
- ```ruby
96
- class Postit < ApplicationRecord
97
- belongs_to :postitcategory, optional: true
98
-
99
- accepts_nested_attributes_for :postitcategory
100
- end
101
-
102
- ```
103
- postcategory.rb
104
- ```ruby
105
- class Postitcategory < ApplicationRecord
106
- has_many :postits
107
- end
108
-
109
- ```
110
-
111
- ## View
112
- ```ruby
113
- <div class="field">
114
- <%= fields_for(:postitcategory) do |c| %>
115
- <%= c.label :category %>
116
- <%= c.text_field :category %>
117
- <% end %>
118
- </div>
119
78
  ```

1

エラー発生

2018/07/26 10:30

投稿

ruby_0ct
ruby_0ct

スコア57

title CHANGED
File without changes
body CHANGED
@@ -75,4 +75,45 @@
75
75
  <%= form.submit %>
76
76
  </div>
77
77
  <% end %>
78
+ ```
79
+
80
+ # 追記 undefined method `build' for nil:NilClassエラー
81
+ has_manyとbelongs_toが逆でしたので修正いたしましたが、buildエラーが発生しました。
82
+ 解決策を頂けると助かります。
83
+ ```
84
+ undefined method `build' for nil:NilClass
85
+ ```
86
+ ## コントローラ
87
+ ```ruby
88
+ def new
89
+ @postit = Postit.new
90
+ @postit.postitcategory.build
91
+ end
92
+ ```
93
+ ##モデル
94
+ postit.rb
95
+ ```ruby
96
+ class Postit < ApplicationRecord
97
+ belongs_to :postitcategory, optional: true
98
+
99
+ accepts_nested_attributes_for :postitcategory
100
+ end
101
+
102
+ ```
103
+ postcategory.rb
104
+ ```ruby
105
+ class Postitcategory < ApplicationRecord
106
+ has_many :postits
107
+ end
108
+
109
+ ```
110
+
111
+ ## View
112
+ ```ruby
113
+ <div class="field">
114
+ <%= fields_for(:postitcategory) do |c| %>
115
+ <%= c.label :category %>
116
+ <%= c.text_field :category %>
117
+ <% end %>
118
+ </div>
78
119
  ```