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

質問編集履歴

1

エラーがでたコードを記述しました。

2020/09/28 07:13

投稿

masaosan18
masaosan18

スコア65

title CHANGED
File without changes
body CHANGED
@@ -42,4 +42,51 @@
42
42
 
43
43
  条件分岐のelsifの部分で、search_office_from_keyword(keywords)というメソッドを呼び出しています。
44
44
 
45
- 解決方法をご存知の方、教えてください。宜しくお願い致します。
45
+ 解決方法をご存知の方、教えてください。宜しくお願い致します。
46
+
47
+
48
+
49
+
50
+ ### 変更したコード
51
+
52
+ ``` ruby
53
+ def search
54
+ if params[:city_id]
55
+ pagy, offices = pagy Office.where(city_id: params[:city_id])
56
+ elsif params[:keyword]
57
+ keywords = params[:keyword].split(/[[:blank:]]+/).select(&:present?)
58
+ offices = like_(keywords.shift)
59
+ keywords.each{ |keyword| offices.or(like_keyword) }
60
+ else
61
+ pagy, offices = pagy(Office.all)
62
+ end
63
+ pagy_headers_merge(pagy)
64
+ render json: offices, each_serializer: OfficeIndexSerializer, include: '**'
65
+ end
66
+
67
+ def like_(keyword)
68
+ Office.where('name LIKE ? OR address LIKE ? OR near_station LIKE ? OR introduction LIKE ? OR company LIKE ?',"%#{keyword}%", "%#{keyword}%", "%#{keyword}%", "%#{keyword}%", "%#{keyword}%")
69
+ end
70
+ ```
71
+
72
+ #### エラー
73
+ ``` ruby
74
+ api_1 | Started GET "/offices/search?keyword=%E6%9D%B1%E4%BA%AC" for 172.21.0.1 at 2020-09-28 07:08:29 +0000
75
+ api_1 | Processing by OfficesController#search as */*
76
+ api_1 | Parameters: {"keyword"=>"東京"}
77
+ api_1 | Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms | Allocations: 696)
78
+ api_1 |
79
+ api_1 |
80
+ api_1 |
81
+ api_1 | NoMethodError (undefined method `prev' for nil:NilClass):
82
+ api_1 |
83
+ api_1 | app/controllers/offices_controller.rb:27:in `search'
84
+ ```
85
+
86
+ 27行目は
87
+ ``` ruby
88
+ pagy_headers_merge(pagy)
89
+ render json: offices, each_serializer: OfficeIndexSerializer, include: '**'
90
+ end
91
+ ```
92
+ 上のコードの```pagy_headers_merge(pagy)```というコードになります。