前提・実現したいこと
Rails6で小説や漫画、映画などのレビューアプリを制作しています。
検索機能を導入したく、参考サイト(https://qiita.com/nojinoji/items/e1b174220da8c81a1756)をもとにransackをインストールして作業を進めていたのですが、エラーが発生しました。
コードが足りない場合はお手数ですがコメントお願いします。追加いたします。
自分の力ではどうにもなりません、、。ご教授お願いします。
発生している問題・エラーメッセージ
先述したサイトを始め様々な情報源をもとに導入を進めていたのですが、どうしても上手くいかず、下記のような画面が出てきます。
ログはこんな感じです。
Started GET "/search" for ::1 at 2019-10-23 02:49:09 +0900 Processing by HomeController#search as HTML User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT ? [["LIMIT", 1]] ↳ app/controllers/application_controller.rb:20:in `set_current_user' Rendering home/search.html.erb within layouts/application Post Load (0.2ms) SELECT DISTINCT "posts".* FROM "posts" LIMIT ? [["LIMIT", 11]] CACHE Post Load (0.0ms) SELECT DISTINCT "posts".* FROM "posts" LIMIT ? [["LIMIT", 11]] CACHE Post Load (0.0ms) SELECT DISTINCT "posts".* FROM "posts" LIMIT ? [["LIMIT", 11]] CACHE Post Load (0.0ms) SELECT DISTINCT "posts".* FROM "posts" LIMIT ? [["LIMIT", 11]] CACHE Post Load (0.0ms) SELECT DISTINCT "posts".* FROM "posts" LIMIT ? [["LIMIT", 11]] CACHE Post Load (0.0ms) SELECT DISTINCT "posts".* FROM "posts" LIMIT ? [["LIMIT", 11]] CACHE Post Load (0.0ms) SELECT DISTINCT "posts".* FROM "posts" LIMIT ? [["LIMIT", 11]] CACHE Post Load (0.0ms) SELECT DISTINCT "posts".* FROM "posts" LIMIT ? [["LIMIT", 11]] Rendered home/search.html.erb within layouts/application (Duration: 247.3ms | Allocations: 213060) Completed 500 Internal Server Error in 251ms (ActiveRecord: 0.7ms | Allocations: 214355)
該当のソースコード
Ruby
1#home/search.html.erb 2 3<body> 4 <%= search_form_for @q do |f| %> 5 <%= f.label :title, "Keyword" %> 6 <%= f.search_field :title_cont %> 7 <%= f.submit "検索" %> 8 <% end %> 9</body>
Controller
Ruby
1#home_controller.rb 2 3class HomeController < ApplicationController 4 5 def review 6 @post = Post.all.order(created_at: :desc) 7 end 8 9 #before_filter :set_search 10 11 def search 12 @q = Post.ransack(params[:q]) 13 @post = Post.all 14 @post = @q.result(distinct: true) 15 end 16 17 def set_search 18 @q = Post.search(search_params) 19 @post = @q.result(distinct: true) 20 end 21 22 private 23 24 def search_params 25 params.require(:q).permit(:title_cont) 26 end 27end
Ruby
1#posts_controller.rb 2 3class PostsController < ApplicationController 4 protect_from_forgery except: :update 5 before_action :ensure_correct_user,{only: [:edit,:update,:destroy]} 6 7 8 def show 9 @id = params[:id] 10 @post = Post.find_by(id: params[:id]) 11 @user = User.find_by(id: @post.user_id) 12 end 13 14 def edit 15 @post = Post.find_by(id: params[:id]) 16 end 17 18 def update 19 @post = Post.find_by(id: params[:id]) 20 @post.title = params[:title] 21 @post.content = params[:content] 22 @post.workname = params[:workname] 23 @post.author = params[:author] 24 @post.style = params[:style] 25 @post.save 26 redirect_to("/") 27 end 28 29 def destroy 30 @post = Post.find_by(id: params[:id]) 31 @post.destroy 32 redirect_to("/") 33 end 34 35 def ensure_correct_user 36 @post = Post.find_by(id:params[:id]) 37 unless @post.user_id == current_user&.id 38 flash[:notice] = "権限がありません" 39 redirect_to("/posts/index") 40 end 41 end 42 43end
postモデル
Ruby
1#post.rb 2 3class Post < ApplicationRecord 4 belongs_to :user 5 6 validates :title, presence: true, length: { in: 3..50 } 7 validates :content, presence: true, length: { in: 10..20000 } 8 validates :workname, presence: true 9 validates :author, presence: true 10end
routes
Ruby
1#routes.rb 2 3Rails.application.routes.draw do 4 5 #-順番変えちゃダメ--------------------------------------- 6 7 # 新規登録・ログイン後のページを指定 8 root to: "home#top" 9 10 # ログイン、アカウント編集後、任意のページに推移させるための記述 11 devise_for :users, :controllers => { 12 :registrations => 'users/registrations', 13 :sessions => 'users/sessions' 14 } 15 16 devise_scope :user do 17 get "signup", :to => "users/registrations#new" 18 get "login", :to => "users/sessions#new" 19 get "logout", :to => "users/sessions#destroy" 20 get 'users/:id/setting', to: 'users/registrations#edit' 21 patch 'users/:id/update', to: 'users/registrations#update' 22 end 23 24 resources :users, only: [:show] 25 26 #---------------------------------------------------- 27 28 # ホームページ 29 get '/' => "home#top" 30 31 # 検索ぺージ 32 get 'search' => "home#search" 33 get 'set_search' => "home#set_search" 34 35 # 新規ヒョーロン投稿関係 36 # 新規投稿アクションと投稿の編集・削除アクションが別のコントローラーで定義されているため、このようなコードになっています。わかりにくくてすみません。 37 get 'posts/new' => "login_user#newpost" 38 post 'posts' => "login_user#create" 39 resources :posts 40 get 'created' => "login_user#postcreate" 41 42 #アカウント編集ページ 43 get 'users/:id/account/edit' => "users#edit" 44 45 # ユーザー詳細ページ 46 get 'users/:id' => "users#show" 47 48 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html 49end 50
Gemfile
Ruby
1#Gemfile 2 3source 'https://rubygems.org' 4git_source(:github) { |repo| "https://github.com/#{repo}.git" } 5 6ruby '2.6.1' 7 8# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 9gem 'rails', '~> 6.0.0' 10# Use sqlite3 as the database for Active Record 11gem 'sqlite3', '~> 1.4' 12# Use Puma as the app server 13gem 'puma', '~> 3.11' 14# Use SCSS for stylesheets 15gem 'sass-rails', '~> 5' 16# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker 17gem 'webpacker', '~> 4.0' 18# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks 19gem 'turbolinks', '~> 5' 20# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 21gem 'jbuilder', '~> 2.7' 22# Use Redis adapter to run Action Cable in production 23# gem 'redis', '~> 4.0' 24# Use Active Model has_secure_password 25# gem 'bcrypt', '~> 3.1.7' 26 27# Use Active Storage variant 28# gem 'image_processing', '~> 1.2' 29 30# Reduces boot times through caching; required in config/boot.rb 31gem 'bootsnap', '>= 1.4.2', require: false 32 33group :development, :test do 34 # Call 'byebug' anywhere in the code to stop execution and get a debugger console 35 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] 36end 37 38group :development do 39 # Access an interactive console on exception pages or by calling 'console' anywhere in the code. 40 gem 'web-console', '>= 3.3.0' 41 gem 'listen', '>= 3.0.5', '< 3.2' 42 # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 43 gem 'spring' 44 gem 'spring-watcher-listen', '~> 2.0.0' 45end 46 47group :test do 48 # Adds support for Capybara system testing and selenium driver 49 gem 'capybara', '>= 2.15' 50 gem 'selenium-webdriver' 51 # Easy installation and use of web drivers to run system tests with browsers 52 gem 'webdrivers' 53end 54 55# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 56gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 57gem 'devise' 58gem 'omniauth' 59gem 'carrierwave' 60gem 'rmagick' 61gem 'jquery-rails' 62gem 'jquery-ui-rails' 63gem 'ransack'
試したこと
@postを@postsに変更
before_filter :set_search をdef set_search … endの直前に追加
<%= search_form_for(@q, url:search_path) do |f| %> を <%= search_form_for @q do |f| %>に変更
以上の3つを試してみましたが、すべて同じエラー画面が表示されました。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/10/23 23:00