実現したいこと
検索結果を表示したい
前提
画像一覧にタイトルがついており、
そのタイトルの部分ヒットで検索結果を表示させたい
発生している問題・エラーメッセージ
エラーメッセージはなく、ただ
何度検索しても全ての結果が表示されます。
該当のソースコード
ruby
1app>controllers>designs_controller.rb 2 3 def search 4 @designs = Design.search(params[:keyword]).order("created_at DESC") 5 end
ruby
1app>models>design.rb 2 3 def self.search(search) 4 if search != "" 5 Design.where('title LIKE(?)', "%#{search}%") 6 else 7 Design.all 8 end 9 end
ruby
1config>routes.rb 2 3 resources :designs do 4 collection do 5 post '/search', to: 'designs#search' 6 get '/search', to: 'designs#search' 7 end 8 end
ruby
1app>views>designs>index.html.rb 2 3 <%= form_with(url: search_designs_path, local:true, scope: :post, class:"search-form") do |form| %> 4 <%= form.text_field :search, placeholder:"検索する", class:"search-input " %> 5 <%= form.submit "検索",class: "search-btn" %> 6 <% end %>
ruby
1log 2 3Completed 404 Not Found in 1ms (ActiveRecord: 0.0ms | Allocations: 376) 4Mar 5 12:37:40 PM I, [2023-03-05T03:37:40.712228 #68] INFO -- : [e1f699d7-f84f-40ec-8a06-d7de78f5b1ab] Started POST "/designs/search" for 172.70.233.183 at 2023-03-05 03:37:40 +0000 5Mar 5 12:37:40 PM I, [2023-03-05T03:37:40.713014 #68] INFO -- : [e1f699d7-f84f-40ec-8a06-d7de78f5b1ab] Processing by DesignsController#search as HTML 6Mar 5 12:37:40 PM I, [2023-03-05T03:37:40.713082 #68] INFO -- : [e1f699d7-f84f-40ec-8a06-d7de78f5b1ab] Parameters: {"authenticity_token"=>"G3jXLcU2oBxzcqmxRRCMl4uFfj+wmKfMSCLoCuGnHy88ENlCH+6fbwU+gAuIHNr4YLR+oXEvEUiXiYRidjoMpw==", "post"=>{"search"=>"クチコミ"}, "commit"=>"検索"} 7Mar 5 12:37:40 PM I, [2023-03-05T03:37:40.745088 #68] INFO -- : [e1f699d7-f84f-40ec-8a06-d7de78f5b1ab] Rendering designs/search.html.erb within layouts/application 8Mar 5 12:37:40 PM D, [2023-03-05T03:37:40.747825 #68] DEBUG -- : [e1f699d7-f84f-40ec-8a06-d7de78f5b1ab] Tag Load (1.2ms) SELECT "tags".* FROM "tags" 9Mar 5 12:37:40 PM D, [2023-03-05T03:37:40.752484 #68] DEBUG -- : [e1f699d7-f84f-40ec-8a06-d7de78f5b1ab] Design Load (3.7ms) SELECT "designs".* FROM "designs" ORDER BY created_at DESC 10Mar 5 12:37:40 PM D, [2023-03-05T03:37:40.756537 #68] DEBUG -- : [e1f699d7-f84f-40ec-8a06-d7de78f5b1ab] ActiveStorage::Attachment Load (1.1ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 59], ["record_type", "Design"], ["name", "image"], ["LIMIT", 1]] 11Mar 5 12:37:40 PM D, [2023-03-05T03:37:40.758089 #68] DEBUG -- : [e1f699d7-f84f-40ec-8a06-d7de78f5b1ab] ActiveStorage::Blob Load (0.9ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2 [["id", 59], ["LIMIT", 1]] 12Mar 5 12:37:40 PM I, [2023-03-05T03:37:40.758877 #68] INFO -- : [e1f699d7-f84f-40ec-8a06-d7de78f5b1ab] Rendered designs/_design.html.erb (Duration: 5.8ms | Allocations: 1220) 13Mar 5 12:37:40 PM D, [2023-03-05T03:37:40.841014 #68] DEBUG -- : [e1f699d7-f84f-40ec-8a06-d7de78f5b1ab] ActiveStorage::Attachment Load (81.3ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id", 58], ["record_type", "Design"], ["name", "image"], ["LIMIT", 1]] 14Mar 5 12:37:40 PM D, [2023-03-05T03:37:40.847695 #68] DEBUG -- : [e1f699d7-f84f-40ec-8a06-d7de78f5b1ab] ActiveStorage::Blob Load (5.8ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2 [["id", 58], ["LIMIT", 1]]
試したこと
web検索し、上記にたどり着いたのですが、ここから
何度やっても検索ができない状態です。
回答2件
あなたの回答
tips
プレビュー