前提・実現したいこと
現在rails で在庫管理のサイトを作成しているところで検索機能を導入し検索できるようにしようとしているよところでエラーなってしまい検索結果が表示できずにいます。
発生している問題・エラーメッセージ
NoMethodError in SearchesController#index undefined method `search' for #<Class:0x00007f84c25e7ec0> Extracted source (around line #3): 1 2 3 4 5 6 class SearchesController < ApplicationController def index @items = Item.search(params[:search]).limit(132) @search = params[:search] end end Rails.root: /Users/yukihiro/stock Application Trace | Framework Trace | Full Trace app/controllers/searches_controller.rb:3:in `index' Request Parameters: {"utf8"=>"✓", "search"=>"松永"}
該当のソースコード
rails
1class SearchesController < ApplicationController 2 def index 3 @items = Item.search(params[:search]).limit(132) 4 @search = params[:search] 5 end 6end 7
.search
.search-container
.search-left
= render 'searches/form-bar'
.search-right
%section.items-box-container
-if @search.present?
%h2.search-result-head
=@search
%span.search-result-head-text
の検索結果
.search-result-number
="1-#{@item.count}件表示"
-else
%h2.search-result-nil
新着商品
.items-box-content
= render @item
Rails.application.routes.draw do root to: "items#index" resources :items, only: [:index, :new,:create, :edit, :destroy, :update, ] resources :searches,only:[:index] end
.top .top_header .top_header_name NETASTOCKS .top_header_search = form_tag(searches_path,method: :get) do = text_field_tag :search, "",placeholder:"何かお探しですか?" = submit_tag 'search', :name => nil 検索 .top_main .top_main_text ネタ在庫一覧 .top_main_btn =link_to "追加",new_item_path .top_main_stock .top_main_stock_box .top_main_stock_box_type .type_name ネタ名 .type_capacity 内容量 .type_unit 単位 .type_quantity 個数 .type_place 場所 .type_delivery 納品日 .type_edit 編集 - @items.each do |item| .top_main_stock_box_item .item_name = item.name .item_capacity = item.capacity .item_unit = item.until .item_quantity = item.quantity .item_place = item.place .item_delivery = item.deliverydate .item_edit =link_to "編集","/items/#{item.id}/edit", method: :get = paginate @items
class ItemsController < ApplicationController PER = 8 def index @items = Item.page(params[:page]).per(PER) 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 destroy item = Item.find(params[:id]) item.destroy redirect_to root_path end def edit @item = Item.find(params[:id]) end def update @item = Item.find(params[:id]) if @item.update(item_params) redirect_to root_path else redirect_to edit_item_path(item.id) end end def self.search(search) return Item.all unless search Item.where(['name LIKE ?', "%#{search}%"]) end
試したこと
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。