前提・実現したいこと
rails 5.2.4を使用し洋服の管理をするアプリケーションを作成中です。
マイページに検索機能を実装しており、マイページにログイン中のユーザーの検索結果のみを表示させたいです。
発生している問題・エラーメッセージ
検索実行時にキーワードが一致すると、他のユーザーの投稿も検索結果として、表示されてしまう。
該当のソースコード
ruby
1# toppモデル 2class Topp < ApplicationRecord 3 belongs_to :user 4 5 def self.search(search) 6 if search 7 Topp.where('category LIKE(?)', "%#{search}%") 8 else 9 Topp.all 10 end 11 end 12 mount_uploader :image, ImageUploader 13end 14 15# topps_controller 16 def show 17 @topp = Topp.find(params[:id]) 18 end 19 20 def edit 21 @topp = Topp.find(params[:id]) 22 redirect_to root_path,notice:"このurlは指定出来ません" unless current_user.id == @topp.user_id 23 end 24 25 def update 26 topp = Topp.find(params[:id]) 27 topp.update(topp_params) 28 redirect_to post_path(current_user),notice: "編集しました" 29 end 30 31 def search 32 @topp_images = Topp.search(params[:keyword]) 33 @bottom_images = current_user.bottoms 34 user = User.find(params[:id]) 35 @nickname = user.nickname 36 end 37 38 private 39 def topp_params 40 params.require(:topp).permit(:image, :size, :maker, :category).merge(user_id: current_user.id) 41 end 42 43 def move_to_index 44 redirect_to root_path, notice: "ログインが必要です"unless user_signed_in? 45 end 46 47end 48 49 50# search.haml 51.wrapper 52 .mypage__title 53 = @nickname 54 sMy-page 55 .search__boxs 56 = form_with(url: search_topp_path, local: true, method: :get) do |form| 57 = form.text_field :keyword, placeholder: "topps category", class: "search__box" 58 = form.submit "serach", class: "search__btn" 59 = form_with(url: search_bottom_path, local: true, method: :get) do |form| 60 = form.text_field :keyword, placeholder: "bottoms category",class: "search__box" 61 = form.submit "serach", class: "search__btn" 62 .topps__container 63 - @topp_images.each do |t_image| 64 = link_to image_tag(t_image.image.to_s, class:"topps__container__image"),"/topps/#{t_image.id}", method: :get 65 .bottoms__container 66 - @bottom_images.each do |b_image| 67 = link_to image_tag(b_image.image.to_s, class:"bottoms__container__image"),"/bottoms/#{b_image.id}", method: :get 68 69 .registration__btns 70 .registration__btns__topps 71 = link_to "Topps-registration",new_topp_path 72 .registration__btns__bottoms 73 = link_to "Bottms-registration",new_bottom_path 74
試したこと
○current_userを@topp_images = Topp.search(params[:keyword])に追加
○margeメソッドを使用しparams(current_user)を追加
○ビューの@topp_imagesに対しcurrent_userを追加
全て何かしらのエラーが出てしまい、丸2日間程詰まっている状況です、、
補足情報(FW/ツールのバージョンなど)
gem'devise'を使用しております
search.hamlの
= form_with(url: search_topp_path, local: true, method: :get) do |form| = form.text_field :keyword, placeholder: "topps category", class: "searchbox" = form.submit "serach", class: "searchbtn"
に検索情報を入力し、
topps.contorollerの
def search @topp_images = Topp.search(params[:keyword]) @bottom_images = current_user.bottoms user = User.find(params[:id]) @nickname = user.nickname end
で@topp_imagesとして情報を受け取り
topp.hamlの
.topps__container - @topp_images.each do |t_image| = link_to image_tag(t_image.image.to_s, class:"topps__container__image"),"/topps/#{t_image.id}", method: :get
@topp_iamgesに情報を受け渡して検索結果を表示しております。この際に検索キーワードが一致していれば、他のユーザーの情報も表示されてしまい困っております。ログインしているユーザーの検索結果のみを表示するにはどのように記載すればよろしいのでしょうか?ご教授頂ければ幸いです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/10/18 04:33