Ruby on Railsでransackを用いた検索機能の実装時のエラー
解決したいこと
現在写真管理アプリみたいなのを作成中で検索機能をつけたく、ransackを用いて実装していました。
しかし検索ボタンを押すとエラーが起きてしましい、解消法がわからず質問に至ったしだいです。
なぜかshowメソッドが動くのはどうしてでしょうか?
アイテムテーブルの中にcategoryカラムを入れておりカテゴリーテーブルとアソシエーションを組んであります。
発生している問題・エラー
ActiveRecord::RecordNotFound in ItemsController#show
Couldn't find Item with 'id'=search
Extracted source (around line #69):
def set_item
@item = Item.find(params[:id])
end
該当するソースコード items_controller.rb
class ItemsController < ApplicationController
before_action :authenticate_user!, except: :index
before_action :set_item, only: [:show, :edit, :update, :destroy]
before_action :set_user, only: [:show, :edit, :update, :destroy]
before_action :search_item, only: [:index, :search]
def index
if user_signed_in?
@item = Item.all.where(user_id: current_user.id)
@item = @item.order('created_at DESC')
set_category_column
end
end
def new
@item = Item.new
end
def create
@item = Item.new(item_params)
if @item.save
redirect_to root_path
else
render 'new'
end
end
def show
end
def edit
end
def update
if @item.update(item_params)
redirect_to item_path(@item.id)
else
render 'edit'
end
end
def destroy
if @item.destroy
redirect_to root_path
else
render 'show'
end
end
def search
@results = @search.result.uncludes(:category) # 検索条件にマッチした商品の情報を取得
binding.pry
end
private
def search_item
@search = Item.ransack(params[:q]) # 検索オブジェクトを生成
end
def item_params
params.require(:item).permit(:explanation, :category_id, :season_id, :brand, :purchase_day, :price, :place, :image).merge(user_id: current_user.id)
end
def set_item
@item = Item.find(params[:id])
end
def set_user
unless user_signed_in? && current_user.id == @item.user_id
redirect_to root_path
end
end
def set_category_column
@category_name = Category.select("name").distinct
end
end
index.html.reb
<%= search_form_for @search, url: items_search_path do |f| %>
<%= f.label :category_name_eq, 'カテゴリー:' %>
<%= f.collection_select :category_name_eq, @category_name, :name, :name %>
<br>
<%= f.submit '検索' %>
<% end %>
自分で試したこと
サーチメソッドにbinding.pryを記述したが止まらないため、メソッドがうまく動いてないのは確認できたのですが
なぜshowメソッドが動くのか分かりません。
未熟者で質問の至らぬ点が多々あるかと思いますが、よろしくお願いします。