最近始めたばかりでecサイト作成中の初心者です
現在検索機能を追加したのですが、検索結果の表示の仕方を変えたいんですが調べてもわからないので質問しました。
今の状況は、縦に3カラムで表示してて。左に検索機能、真ん中に商品情報等(検索結果の表示場所)、右にカートという感じで作っていて、真ん中のカラムに<%= yield %>で表示を切り替えて使っている感じです。
そこで質問なのですが、検索した結果の表示を編集したいのですが方法がわからず困っています。
ルート画面や、検索結果画面はstore/index.html.erbを使っています。
表示したいのは真ん中のカラムに検索結果と検索結果の数です。それと、検索結果がない時にメッセージ表示したい考えています。
今の所、真ん中に検索結果のみ表示されてるのでその際に検索の数と無かった場合のテキストを加えたいと思い質問させていただいております。
class Product < ApplicationRecord scope :get_by_name, ->(title){ where('title like ?',"%#{title}%") } end
class ProductsController < ApplicationController def index @products = Product.all if params[:title].present? @products = @products.get_by_name params[:title] end end end
class StoreController < ApplicationController def index @products = Product.paginate(:page => params[:page], :per_page => 12) if params[:title].present? @products = @products.get_by_name params[:title] end end
application.html.erbの検索部分
<div class="search border1"> <%= form_tag(store_path, method: "get") do %> <%= text_field_tag :title, params[:title], :size => "37" %> <%= submit_tag "検索" %> <% end %> </div>
おそらくstore.controllerのifのところにelseでかくといいと思うんですけど、何書いたらいいのか全くわかりませんでした。
もしかしたら新しくoo.html.erbファイルの作らないとダメなのかも?とも思ってきています。
以上です。このような感じで申し訳ないのですが、何かお気付きの方よろしくお願いします。
あなたの回答
tips
プレビュー