いま作っている映画情報のWEBサイトに検索機能を付けたいです。
入力されたキーワードが作品タイトルに含まれている作品の検索をしたいのですが、
ActiveRecord::RecordNotFound in ProductsController#search
Couldn't find Product with 'id'= とエラーが出ました。
products_controller.rbのsearchアクションに、検索フォームのキーワードをあいまい検索して、productsテーブルから20件の作品情報を取得する処理を書こうと思い、
products_controller.rbに
ruby
1class ProductsController < RankingController 2 def index 3 # productsテーブルから最新順に作品を20件取得する 4 @products = Product.limit(20).order('id DESC') 5 end 6 7 def show 8 @product = Product.find(params[:id]) 9 end 10 11 def search 12 @products=Product.find(params[:title]).order('id DESC').limit(20) 13 @products =Product.where("name like '%" + title + "%'") 14 end 15end
と記述しました。
データベースには映画のタイトルごとにidが付与されていて、そのデータベースを収納しているProductテーブルからtitleの情報を取得したかったです。
searchアクションのparams[:title]はidが設定される想定ですか?
params[:title]にはidが設定されないように考えていました。