前提・実現したいこと
本を投稿するアプリを実装中に下記エラーが発生しました。
検索結果の表示でtitle,body,categoryの3つをそれぞれ検索できるようにしたいです。
発生している問題・エラーメッセージ
ArgumentError in HomesController#search wrong number of arguments (given 0, expected 1) Extracted source (around line #12): 12 def Book.search(search_word) Book.where('title LIKE(?) OR body LIKE(?) OR category LIKE(?)', "%#{search}%", "%#{search}%") end
該当のソースコード
schema
1 create_table "books", force: :cascade do |t| 2 t.string "title" 3 t.string "image_id" 4 t.text "body" 5 t.integer "user_id" 6 t.datetime "created_at", null: false 7 t.datetime "updated_at", null: false 8 t.float "rate" 9 t.integer "category_id" 10 t.string "category" 11 end
BookModel
1class Book < ApplicationRecord 2 belongs_to :user 3 has_many :favorites, dependent: :destroy 4 has_many :book_comments, dependent: :destroy 5 has_many :reviews, dependent: :destroy 6 has_many :categories, dependent: :destroy 7 8 def favorited_by?(user) 9 favorites.where(user_id: user.id).exists? 10 end 11 12 def Book.search(search_word) 13 Book.where('title LIKE(?) OR body LIKE(?) OR category LIKE(?)', "%#{search}%", "%#{search}%", "%#{search}%") 14 end 15 16 validates :title, presence: true 17 validates :body, presence: true, length: {maximum: 200} 18end 19
HomesController
1class HomesController < ApplicationController 2 def top 3 end 4 5 def about 6 end 7 8 def search 9 @book = Book.new 10 @books = Book.search(params[:keyword]) 11 end 12 13 14end 15
applicationhtmlerb
1 <!--検索--> 2 <%= form_with url: search_path, method: :get, local: :true do |f| %> 3 <%= f.text_field :keyword %> 4 <%= f.submit "Search", class: "btn btn-primary" %> 5 <% end %>
試したこと
カラムがあるかどうかの確認
下記サイト参照にして実装
補足情報(FW/ツールのバージョンなど)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/08/02 01:16