質問編集履歴

2

indexアクション追加

2020/06/17 23:06

投稿

pecchan
pecchan

スコア555

test CHANGED
File without changes
test CHANGED
@@ -54,7 +54,43 @@
54
54
 
55
55
  ```ruby
56
56
 
57
+ class SubCategoriesController < ApplicationController
58
+
59
+
60
+
61
+ def index
62
+
57
- @sub_categories = SubCategory.where(category_id: params[:category_id]).includes(:items).all
63
+ @sub_categories = SubCategory.where(category_id: params[:category_id]).includes(:items)
64
+
65
+
66
+
67
+ respond_to do |format|
68
+
69
+ # カテゴリ一覧から呼び出し
70
+
71
+ format.html do
72
+
73
+ end
74
+
75
+
76
+
77
+ # セレクトボックスから呼び出し
78
+
79
+ format.js do
80
+
81
+ render json: @sub_categories.select(:id, :name)
82
+
83
+ end
84
+
85
+ end
86
+
87
+
88
+
89
+ end
90
+
91
+
92
+
93
+ end
58
94
 
59
95
  ```
60
96
 

1

log追加

2020/06/17 23:06

投稿

pecchan
pecchan

スコア555

test CHANGED
File without changes
test CHANGED
@@ -79,3 +79,51 @@
79
79
  @sub_categoriesがnilのためビューの1行目でエラーになります。
80
80
 
81
81
  「undefined method `each' for nil:NilClass」
82
+
83
+
84
+
85
+ 2020/06/17追記
86
+
87
+ SubCategoryにアクセスした時のログ(allは外しました)
88
+
89
+ ```log
90
+
91
+ Started GET "/sub_categories/1" for 58.13.43.197 at 2020-06-17 14:04:53 +0000
92
+
93
+ Cannot render console from 58.13.43.197! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
94
+
95
+ Processing by SubCategoriesController#index as HTML
96
+
97
+ Parameters: {"category_id"=>"1"}
98
+
99
+ Rendering sub_categories/index.html.erb within layouts/application
100
+
101
+ Rendered sub_categories/index.html.erb within layouts/application (2.4ms)
102
+
103
+ Completed 500 Internal Server Error in 11ms (ActiveRecord: 0.3ms)
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+ ActionView::Template::Error (undefined method `each' for nil:NilClass):
112
+
113
+ 1:
114
+
115
+ 2: <% @sub_categories.each do |sub_category| %>
116
+
117
+ 3: <p><%= link_to sub_category.name, items_path(search: {sub_category_id: sub_category.id}) %> <%= "(#{sub_category.items.count})" %></p>
118
+
119
+ 4:
120
+
121
+ 5: <% end %>
122
+
123
+
124
+
125
+ app/views/sub_categories/index.html.erb:2:in `_app_views_sub_categories_index_html_erb___733152297473355794_70113344435740'
126
+
127
+
128
+
129
+ ```