前提・実現したいこと
ここに質問の内容を詳しく書いてください。
ruby on railsでYahoo!知恵袋のようなシステムを作っています。
記事検索機能を実装中に以下のエラーが発生しました。
原因と解決策をご教示お願いいたします。
発生している問題・エラーメッセージ
検索ボタンを押下しても検索結果が現れず、全ての投稿が表示される models/post.rbで記載してあるsearchメソッドがいかなる場合でもPost.allを取得してしまう
該当のソースコード
models/post.rb
class Post < ApplicationRecord attr_accessor :keyword has_many :comments belongs_to :user def self.search(search) return Post.all unless search Post.where('text LIKE(?)', "%#{search}%") end end
views/posts/searches/index.html.haml
.contents .content__title 検索結果 - @posts.each do |post| = render partial: "shared/post", locals: { post: post }
views/posts/shared/_post.html.haml
.content .content__upper .content__upper-icon = icon('fas', 'user-alt', class: 'icon') .content__upper__right .content__upper__right-name = link_to post.user.name, user_path(post.user), class: "content__upper__right-name" .content__upper__right__element .content__upper__right__element-industry - if post.industry? 希望業界: = post.industry .content__upper__right__element-hopejob - if post.hopejob? 希望職種: = post.hopejob .content__upper__right__element-nowjob - if post.nowjob? 現職: = post.nowjob .content__upper__right-date = post.created_at.strftime("%Y/%m/%d(%a) %H:%M") .content__lower %p.content__lower__text = link_to post.text.truncate(200), post_path(post.id), class: "content__lower__text"
controllers/posts_controller.rb
class PostsController < ApplicationController before_action :set_post, only: [:edit, :show] def index @post = Post.new @posts = Post.includes(:user).page(params[:page]).per(5).order("created_at DESC") end 〜 中略 〜 def search @posts = Post.search(params[:keyword]) end private def post_params params.require(:post).permit(:industry,:hopejob ,:nowjob, :text, :solved_status).merge(user_id: current_user.id) end def set_post @post = Post.find(params[:id]) end end
controllers/posts/searches_controller.rb
class Posts::SearchesController < ApplicationController def index @post = Post.new @posts = Post.search(params[:keyword]) end end
config/routes
Rails.application.routes.draw do devise_for :users root "posts#index" namespace :posts do resources :searches, only: :index end resources :posts do resources :comments, only: :create end end
###追加情報
views/layouts/application.html.haml
%body %header.header .header__title = link_to "Self PR", root_path, class: "header__title--text" = form_for(@post, url: posts_searches_path, method: :get, class: "search-form") do |f| = f.text_field :keyword, placeholder: "記事を検索する", class: "search-form__input" = icon('fas', 'search', class: 'search-icon') = f.submit "検索", class: "search-form__btn" .header__right - if user_signed_in? = link_to current_user.name, user_path(current_user), class: "header__right--btn" = link_to "新規投稿", new_post_path, class: "header__right--btn" = link_to "ログアウト", destroy_user_session_path, method: :delete, class: "header__right--btn" - else = link_to "ログイン", new_user_session_path, class: "header__right--btn" = link_to "新規登録", new_user_registration_path, class: "header__right--btn" = yield
sample
と検索時のターミナルのログ
Started GET "/posts/searches?utf8=%E2%9C%93&post%5Bkeyword%5D=sample&commit=%E6%A4%9C%E7%B4%A2" for ::1 at 2019-12-14 12:31:19 +0900 Processing by Posts::SearchesController#index as HTML Parameters: {"utf8"=>"✓", "post"=>{"keyword"=>"sample"}, "commit"=>"検索"} User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1 Rendering posts/searches/index.html.haml within layouts/application Post Load (0.2ms) SELECT `posts`.* FROM `posts` User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1 Rendered shared/_post.html.haml (5.5ms) CACHE (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1 [["id", 2], ["LIMIT", 1]] Rendered shared/_post.html.haml (1.3ms) CACHE (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1 [["id", 2], ["LIMIT", 1]] Rendered shared/_post.html.haml (1.7ms) Rendered posts/searches/index.html.haml within layouts/application (16.2ms) Completed 200 OK in 58ms (Views: 52.6ms | ActiveRecord: 0.7ms)
補足情報(FW/ツールのバージョンなど)
Rails 5.0.7.2
form_forメソッドを使用しています。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/12/14 07:25
2019/12/14 07:42