質問編集履歴

2

検索フォーム追加させていただきました。

2021/08/16 04:54

投稿

nao0725
nao0725

スコア5

test CHANGED
File without changes
test CHANGED
@@ -364,7 +364,7 @@
364
364
 
365
365
 
366
366
 
367
- ````
367
+ ```
368
368
 
369
369
  ActiveRecord::StatementInvalid in Public::Homes#search
370
370
 
@@ -376,9 +376,25 @@
376
376
 
377
377
 
378
378
 
379
- ````
379
+ ```
380
+
381
+
382
+
380
-
383
+ ```
384
+
381
-
385
+ <%= form_with url: search_path, method: :get, local: true do |f| %>
386
+
387
+ <%= f.text_field :keyword,placeholder: "キーワードで検索", class: "search-text"
388
+
389
+ <%= button_tag type: "submit", class: "btn btn-default" do %>
390
+
391
+ <i class="fas fa-search"></i>
392
+
393
+ <% end %>
394
+
395
+ <% end %>
396
+
397
+ ```
382
398
 
383
399
 
384
400
 

1

view,Controller,エラー詳細を載せました

2021/08/16 04:54

投稿

nao0725
nao0725

スコア5

test CHANGED
File without changes
test CHANGED
@@ -210,6 +210,174 @@
210
210
 
211
211
 
212
212
 
213
+ home_controller.rb
214
+
215
+ ````
216
+
217
+ def search
218
+
219
+ @article = Article.new
220
+
221
+ @articles = Article.search(params[:keyword])
222
+
223
+ .page(params[:page]).per(10)
224
+
225
+ @bookmarks = Article.find(Bookmark.group(:article_id)
226
+
227
+ .order(Arel.sql("count(article_id) desc"))
228
+
229
+ .pluck(:article_id))
230
+
231
+ @reviews = Article.find(Comment.group(:article_id)
232
+
233
+ .order(Arel.sql("avg(rate) desc"))
234
+
235
+ .pluck(:article_id))
236
+
237
+ end
238
+
239
+ ````
240
+
241
+
242
+
243
+ search.html.erb
244
+
245
+
246
+
247
+ ````
248
+
249
+ <div class="container">
250
+
251
+ <p id="notice"><%= notice %></p>
252
+
253
+
254
+
255
+ <h1>みんなのノウハウ</h1>
256
+
257
+
258
+
259
+ <% if @articles.exists? %>
260
+
261
+
262
+
263
+ <div class="tab-area">
264
+
265
+ <div class="tab active">
266
+
267
+ 新着投稿
268
+
269
+ </div>
270
+
271
+ <div class="tab">
272
+
273
+ ブックマーク数順
274
+
275
+ </div>
276
+
277
+ <div class="tab">
278
+
279
+ 評価の高い順
280
+
281
+ </div>
282
+
283
+ </div>
284
+
285
+
286
+
287
+ <div class="content-area">
288
+
289
+
290
+
291
+ <!--新規投稿順-->
292
+
293
+ <div class="content show">
294
+
295
+ <% @articles.each do |article| %>
296
+
297
+ <%= render "public/articles/index", article: article %>
298
+
299
+ <% end %>
300
+
301
+ <div class = "pagination-sm">
302
+
303
+ <%= paginate @articles %>
304
+
305
+ </div>
306
+
307
+ </div>
308
+
309
+
310
+
311
+ <!--ブックマーク数順-->
312
+
313
+ <div class="content">
314
+
315
+ <% @bookmarks.each.with_index(1) do |article, i| %>
316
+
317
+ <%= render "public/articles/index", article: article %>
318
+
319
+ <% end %>
320
+
321
+ </div>
322
+
323
+
324
+
325
+ <!--評価の高い順-->
326
+
327
+ <div class="content">
328
+
329
+ <% @reviews.each.with_index(1) do |article, i| %>
330
+
331
+ <%= render "public/articles/index", article: article %>
332
+
333
+ <% end %>
334
+
335
+ </div>
336
+
337
+ </div>
338
+
339
+
340
+
341
+ <% else %>
342
+
343
+ <p>該当する記事は見つかりませんでした。</p>
344
+
345
+ <% end %>
346
+
347
+
348
+
349
+ <%= link_to "HOME", home_path %>
350
+
351
+ </div>
352
+
353
+
354
+
355
+
356
+
357
+
358
+
359
+ ````
360
+
361
+
362
+
363
+ エラー詳細です
364
+
365
+
366
+
367
+ ````
368
+
369
+ ActiveRecord::StatementInvalid in Public::Homes#search
370
+
371
+ Showing /home/ec2-user/environment/Teach_Market/app/views/public/homes/search.html.erb where line #6 raised:
372
+
373
+
374
+
375
+ SQLite3::SQLException: no such column: tag_name: SELECT 1 AS one FROM "articles" WHERE (title LIKE('%営業%') OR body LIKE('%営業%') OR tag_name LIKE('%営業%') ) LIMIT ? OFFSET ?
376
+
377
+
378
+
379
+ ````
380
+
213
381
 
214
382
 
215
383
 
@@ -218,7 +386,7 @@
218
386
 
219
387
 
220
388
 
221
- Articleモデルへtag_nameの検索を追加 → Nameerrorで怒られる
389
+ Articleモデルへtag_nameの検索を追加 → StatementInvalid で怒られる
222
390
 
223
391
  Tagモデルへ検索メソッドを追記 → 変化なし
224
392