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

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

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

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

Q&A

解決済

1回答

971閲覧

Ruby on rails で画像の検索機能を実装したい

tamtamtime

総合スコア8

Ruby on Rails 6

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

0グッド

0クリップ

投稿2023/01/07 07:04

前提

検索機能の付いた画像一覧を作成中です。
画像それぞれに詳細ページがあります。

実現したいこと

「検索」ボタンで、キーワードから該当画像を一覧で出したいです。

発生している問題・エラーメッセージ

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 index 9 @d_production = Dproduction.includes(:user).order("created_at DESC") 10 end 11 12 def new 13 @d_productions = Dproduction.new 14 end 15 16 def create 17 @d_productions = Dproduction.create(d_production_params) 18 if 19 @d_productions.save 20 redirect_to root_path 21 else 22 render :new 23 end 24 end 25 26 def show 27 28 end 29 30 def destroy 31 d_production = Dproduction.find(params[:id]) 32 if d_production.destroy 33 redirect_to root_path 34 end 35 end 36 37 def update 38 d_production = Dproduction.find(params[:id]) 39 if 40 d_production.update(d_production_params) 41 redirect_to d_production_path(dproduction.id) 42 else 43 redirect_to request.referer 44 end 45 end 46 47 def edit 48 unless user_signed_in? && current_user.id == @d_production.user_id 49 redirect_to action: :index 50 end 51 end 52 53 def search 54 @d_production = Dproduction.search(params[:keyword]).order("created_at DESC") 55 end 56 57 private 58 59 def d_production_params 60 params.permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) 61 end 62 63 def set_dproduction 64 @d_production = Dproduction.find(params[:id]) 65 end 66 67 def move_to_index 68 unless user_signed_in? 69 redirect_to action: :index 70 end 71 end 72 73end

ruby

1(models > dproduction.rb) 2class Dproduction < ApplicationRecord 3 belongs_to :user 4 has_many :comments, dependent: :destroy 5 has_one_attached :image 6 7 validates :title, presence: true 8 validates :catch_copy, presence: true 9 validates :concept, presence: true 10 validates :image, presence: true 11 12 def self.search(search) 13 if search != "" 14 Dproductions.where('text LIKE(?)', "%#{search}%") 15 else 16 Dproductions.all 17 end 18 end 19end 20

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です。

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

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

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

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

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

yuma.inaura

2023/01/07 07:15

params[:id] の値はどうなってますか?
yuma.inaura

2023/01/07 07:18

どんなタイミングでエラーが起こるのでしょうか?
tamtamtime

2023/01/07 07:24

「検索」を押した時にエラーが出ます。
tamtamtime

2023/01/07 07:27

Dproduction.find(params[:id])で Traceback (most recent call last): 1: from (irb):1 NameError (undefined local variable or method `params' for main:Object) と出ます。SequelProでは各画像にidが入っているのですが、やはりidがおかしいのでしょうか? 初歩的で申し訳ございません。ご教示いただけますと幸いです。
tamtamtime

2023/01/07 07:39 編集

@d_productionとすると「nil」が返ってきますが 「Dproduction.find(3)」とすると 中身が出てきます。
yuma.inaura

2023/01/07 07:38

どの行でそのデバッグプリントを実行したんでしょう? show でエラーが起きているように見えるんですが、showに検索フォームがあるのでしょうか?
yuma.inaura

2023/01/07 07:40

>@d_productionとすると「nil」が返ってきますが 「とすると」というのはどんなことなんでしょうか?
tamtamtime

2023/01/07 07:41 編集

検索フォームはindexにあります。 ``` <main class="main"> <div class="inner"> <%= form_with(url: search_d_productions_path, local:true, method: :get, class:"search-form") do |form| %> <%= form.text_field :keyword, placeholder:"検索する", class:"search-input" %> <%= form.submit "検索",class: "search-btn" %> <% end %> <div class="card__wrapper"> <%= render partial: 'dproduction', collection: @d_production %> </div> </div> </main> ```
yuma.inaura

2023/01/07 07:41

検索実行時にコントローラーのsearchアクションは実行されてそうですか?
tamtamtime

2023/01/07 07:42

失礼しました、ターミナルのコマンドで@d_productionとすると、nilが返ってきます。
tamtamtime

2023/01/07 07:44

ターミナルには ActiveRecord::RecordNotFound (Couldn't find Dproduction with 'id'=search): app/controllers/d_productions_controller.rb:63:in `set_dproduction' Started GET "/d_productions/search?keyword=%E6%9C%9D%E9%A3%9F&commit=%E6%A4%9C%E7%B4%A2" for ::1 at 2023-01-07 16:42:50 +0900 Processing by DProductionsController#show as HTML Parameters: {"keyword"=>"朝食", "commit"=>"検索", "id"=>"search"} Dproduction Load (0.4ms) SELECT `dproductions`.* FROM `dproductions` WHERE `dproductions`.`id` = NULL LIMIT 1 ↳ app/controllers/d_productions_controller.rb:63:in `set_dproduction' Completed 404 Not Found in 1ms (ActiveRecord: 0.4ms | Allocations: 1389) と出ておりますので、searchアクションは実行されているのではないかと思います。
yuma.inaura

2023/01/07 07:45

>Processing by DProductionsController#show as HTML showが実行されてませんか?
tamtamtime

2023/01/07 07:53 編集

ActiveRecord::RecordNotFound in DProductionsController#show すみません、エラー画面で上記が出ますので、showを実行してエラーになったという事かもしれません。 とすると、searchアクションそのものが動いていないという事でしょうか?
yuma.inaura

2023/01/07 08:00

>searchアクションそのものが動いていないという事でしょうか? っぽいですよね $ rails routes でルーティングを再確認するなどしたら良いかもしれません
tamtamtime

2023/01/07 08:08

ありがとうございます!ルート確認しますと new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format)  devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit user_password PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update POST /users/password(.:format) devise/passwords#create cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel new_user_registration GET /users/sign_up(.:format) devise/registrations#new edit_user_registration GET /users/edit(.:format) devise/registrations#edit user_registration PATCH /users(.:format) devise/registrations#update PUT /users(.:format) devise/registrations#update DELETE /users(.:format) devise/registrations#destroy POST /users(.:format) devise/registrations#create root GET / d_productions#index d_productions GET /d_productions(.:format) d_productions#index POST /d_productions(.:format) d_productions#create new_d_production GET /d_productions/new(.:format) d_productions#new edit_d_production GET /d_productions/:id/edit(.:format) d_productions#edit d_production GET /d_productions/:id(.:format) d_productions#show PATCH /d_productions/:id(.:format) d_productions#update PUT /d_productions/:id(.:format) d_productions#update DELETE /d_productions/:id(.:format) d_productions#destroy search_d_productions GET /d_productions/search(.:format) d_productions#search GET /d_productions(.:format) d_productions#index POST /d_productions(.:format) d_productions#create GET /d_productions/new(.:format) d_productions#new GET /d_productions/:id/edit(.:format) d_productions#edit GET /d_productions/:id(.:format) d_productions#show PATCH /d_productions/:id(.:format) d_productions#update PUT /d_productions/:id(.:format) d_productions#update DELETE /d_productions/:id(.:format) d_productions#destroy user GET /users/:id(.:format) users#show d_productions_new POST /d_productions/new(.:format) d_productions#create rails_postmark_inbound_emails POST /rails/action_mailbox/postmark/inbound_emails(.:format) action_mailbox/ingresses/postmark/inbound_emails#create rails_relay_inbound_emails POST /rails/action_mailbox/relay/inbound_emails(.:format) action_mailbox/ingresses/relay/inbound_emails#create rails_sendgrid_inbound_emails POST /rails/action_mailbox/sendgrid/inbound_emails(.:format) action_mailbox/ingresses/sendgrid/inbound_emails#create rails_mandrill_inbound_health_check GET /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#health_check rails_mandrill_inbound_emails POST /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#create rails_mailgun_inbound_emails POST /rails/action_mailbox/mailgun/inbound_emails/mime(.:format) action_mailbox/ingresses/mailgun/inbound_emails#create rails_conductor_inbound_emails GET /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#index POST /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#create new_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/new(.:format) rails/conductor/action_mailbox/inbound_emails#new edit_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format) rails/conductor/action_mailbox/inbound_emails#edit rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#show PATCH /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update PUT /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#destroy rails_conductor_inbound_email_reroute POST /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format) rails/conductor/action_mailbox/reroutes#create rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create と出ました。search_d_productionsは確認できたのですが…。
winterboum

2023/01/07 10:01

config/routes.rb に問題があります。 載せてください
tamtamtime

2023/01/07 23:44

回答遅くなり申し訳ございません。以下がroutes.rbになります。 Rails.application.routes.draw do devise_for :users root to: 'd_productions#index' resources :d_productions resources :d_productions do collection do get 'search' end end resources :users, only: :show post '/d_productions/new', to: 'd_productions#create' end
guest

回答1

0

ベストアンサー

code はコメントにではなく。質問を編集してそちらに載せ替えてください。
インデントもなくなってしまってる。

resources :d_productions # <= ここと resources :d_productions do #<= ここで collection do get 'search' end

ダブって定義されてます。
このため 2つ目の get search より 1つ目の get show の方が先にhitするので、そういうけっかになります。
2つ目だけにしましょう

投稿2023/01/08 14:11

winterboum

総合スコア23347

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

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

tamtamtime

2023/01/09 00:26

失礼いたしました、ご回答ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問