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

質問編集履歴

2

indexアクション追加

2020/06/17 23:06

投稿

pecchan
pecchan

スコア592

title CHANGED
File without changes
body CHANGED
@@ -26,7 +26,25 @@
26
26
 
27
27
  SubCategory コントローラ
28
28
  ```ruby
29
+ class SubCategoriesController < ApplicationController
30
+
31
+ def index
29
- @sub_categories = SubCategory.where(category_id: params[:category_id]).includes(:items).all
32
+ @sub_categories = SubCategory.where(category_id: params[:category_id]).includes(:items)
33
+
34
+ respond_to do |format|
35
+ # カテゴリ一覧から呼び出し
36
+ format.html do
37
+ end
38
+
39
+ # セレクトボックスから呼び出し
40
+ format.js do
41
+ render json: @sub_categories.select(:id, :name)
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+ end
30
48
  ```
31
49
 
32
50
 

1

log追加

2020/06/17 23:06

投稿

pecchan
pecchan

スコア592

title CHANGED
File without changes
body CHANGED
@@ -38,4 +38,28 @@
38
38
  ```
39
39
 
40
40
  @sub_categoriesがnilのためビューの1行目でエラーになります。
41
- 「undefined method `each' for nil:NilClass」
41
+ 「undefined method `each' for nil:NilClass」
42
+
43
+ 2020/06/17追記
44
+ SubCategoryにアクセスした時のログ(allは外しました)
45
+ ```log
46
+ Started GET "/sub_categories/1" for 58.13.43.197 at 2020-06-17 14:04:53 +0000
47
+ Cannot render console from 58.13.43.197! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
48
+ Processing by SubCategoriesController#index as HTML
49
+ Parameters: {"category_id"=>"1"}
50
+ Rendering sub_categories/index.html.erb within layouts/application
51
+ Rendered sub_categories/index.html.erb within layouts/application (2.4ms)
52
+ Completed 500 Internal Server Error in 11ms (ActiveRecord: 0.3ms)
53
+
54
+
55
+
56
+ ActionView::Template::Error (undefined method `each' for nil:NilClass):
57
+ 1:
58
+ 2: <% @sub_categories.each do |sub_category| %>
59
+ 3: <p><%= link_to sub_category.name, items_path(search: {sub_category_id: sub_category.id}) %> <%= "(#{sub_category.items.count})" %></p>
60
+ 4:
61
+ 5: <% end %>
62
+
63
+ app/views/sub_categories/index.html.erb:2:in `_app_views_sub_categories_index_html_erb___733152297473355794_70113344435740'
64
+
65
+ ```