質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

1回答

1085閲覧

railsで検索機能 あいまい検索を実装中

yukihiro_3710

総合スコア6

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/02/27 06:34

前提・実現したいこと

現在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/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

Railsのモデルには search というmethodはありません。
Ransackというgemを入れればあります。
が、使い方がちょっとちがいます。

参考にしているシステムとGemfileを比べてみてください
もしくは Itemにmethodを定義するか、

投稿2020/02/27 17:36

winterboum

総合スコア23347

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問