前提
検索機能の付いた画像一覧を作成中です。
画像それぞれに詳細ページがあります。
実現したいこと
「検索」ボタンで、キーワードから該当画像を一覧で出したいです。
発生している問題・エラーメッセージ
ActiveRecord::RecordNotFound in DProductionsController#show Couldn't find Dproduction with 'id'=search Extracted source (around line #63): 61 62 def set_dproduction 63 @d_production = Dproduction.find(params[:id]) 64 end 65 66 def move_to_index
該当のソースコード
ruby
1(d_productions_controller.rb)2class DProductionsController < ApplicationController 3 before_action :set_dproduction, only: [:show, :edit]4 before_action :authenticate_user!, only:[:edit, :new, :destroy]5 before_action :move_to_index, except: [:index, :show, :search]6 7 8 def index9 @d_production = Dproduction.includes(:user).order("created_at DESC")10 end11 12 def new13 @d_productions = Dproduction.new14 end15 16 def create17 @d_productions = Dproduction.create(d_production_params)18 if 19 @d_productions.save 20 redirect_to root_path 21 else22 render :new23 end24 end25 26 def show27 28 end29 30 def destroy31 d_production = Dproduction.find(params[:id])32 if d_production.destroy 33 redirect_to root_path 34 end35 end36 37 def update38 d_production = Dproduction.find(params[:id])39 if40 d_production.update(d_production_params)41 redirect_to d_production_path(dproduction.id)42 else43 redirect_to request.referer 44 end45 end46 47 def edit48 unless user_signed_in? && current_user.id == @d_production.user_id 49 redirect_to action: :index50 end51 end52 53 def search54 @d_production = Dproduction.search(params[:keyword]).order("created_at DESC")55 end56 57 private58 59 def d_production_params60 params.permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id)61 end62 63 def set_dproduction64 @d_production = Dproduction.find(params[:id])65 end66 67 def move_to_index68 unless user_signed_in?69 redirect_to action: :index70 end71 end72 73end
ruby
1(models > dproduction.rb)2class Dproduction < ApplicationRecord3 belongs_to :user4 has_many :comments, dependent: :destroy5 has_one_attached :image6 7 validates :title, presence: true8 validates :catch_copy, presence: true9 validates :concept, presence: true10 validates :image, presence: true11 12 def self.search(search)13 if search != ""14 Dproductions.where('text LIKE(?)', "%#{search}%")15 else16 Dproductions.all 17 end18 end19end20
ruby(search.html.erb)
1<%= form_with(url: search_d_productions_path, local: true, method: :get, class: "search-form") do |form| %> 2 <%= form.text_field :keyword, placeholder: "投稿を検索する", class: "search-input" %> 3 <%= form.submit "検索", class: "search-btn" %>4<% end %> 5<div class="contents row">6 <% @d_productions.each do |d_production| %> 7 <%= render partial: "d_production", locals: { d_production: d_production } %>8 <% end %> 9</div>
試したこと
コントローラーの
def set_dproduction
@d_production = Dproduction.find(params[:id])
end
がおかしいのかと色々調べてやってみたのですが、どれも上手くいきませんでした。
補足情報(FW/ツールのバージョンなど)
rails _6.0.0です。

0 コメント