質問編集履歴

1

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

2020/09/28 07:13

投稿

masaosan18
masaosan18

スコア64

test CHANGED
File without changes
test CHANGED
@@ -87,3 +87,97 @@
87
87
 
88
88
 
89
89
  解決方法をご存知の方、教えてください。宜しくお願い致します。
90
+
91
+
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+ ### 変更したコード
100
+
101
+
102
+
103
+ ``` ruby
104
+
105
+ def search
106
+
107
+ if params[:city_id]
108
+
109
+ pagy, offices = pagy Office.where(city_id: params[:city_id])
110
+
111
+ elsif params[:keyword]
112
+
113
+ keywords = params[:keyword].split(/[[:blank:]]+/).select(&:present?)
114
+
115
+ offices = like_(keywords.shift)
116
+
117
+ keywords.each{ |keyword| offices.or(like_keyword) }
118
+
119
+ else
120
+
121
+ pagy, offices = pagy(Office.all)
122
+
123
+ end
124
+
125
+ pagy_headers_merge(pagy)
126
+
127
+ render json: offices, each_serializer: OfficeIndexSerializer, include: '**'
128
+
129
+ end
130
+
131
+
132
+
133
+ def like_(keyword)
134
+
135
+ Office.where('name LIKE ? OR address LIKE ? OR near_station LIKE ? OR introduction LIKE ? OR company LIKE ?',"%#{keyword}%", "%#{keyword}%", "%#{keyword}%", "%#{keyword}%", "%#{keyword}%")
136
+
137
+ end
138
+
139
+ ```
140
+
141
+
142
+
143
+ #### エラー
144
+
145
+ ``` ruby
146
+
147
+ 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
148
+
149
+ api_1 | Processing by OfficesController#search as */*
150
+
151
+ api_1 | Parameters: {"keyword"=>"東京"}
152
+
153
+ api_1 | Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms | Allocations: 696)
154
+
155
+ api_1 |
156
+
157
+ api_1 |
158
+
159
+ api_1 |
160
+
161
+ api_1 | NoMethodError (undefined method `prev' for nil:NilClass):
162
+
163
+ api_1 |
164
+
165
+ api_1 | app/controllers/offices_controller.rb:27:in `search'
166
+
167
+ ```
168
+
169
+
170
+
171
+ 27行目は
172
+
173
+ ``` ruby
174
+
175
+ pagy_headers_merge(pagy)
176
+
177
+ render json: offices, each_serializer: OfficeIndexSerializer, include: '**'
178
+
179
+ end
180
+
181
+ ```
182
+
183
+ 上のコードの```pagy_headers_merge(pagy)```というコードになります。