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

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

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

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

Ruby on Rails

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

Q&A

解決済

2回答

364閲覧

検索機能実装中にActiveRecord::RecordNotFoundエラー

yamada_yuuki

総合スコア100

Ruby on Rails 5

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

Ruby on Rails

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

0グッド

1クリップ

投稿2019/11/05 07:54

https://qiita.com/yusuko/items/cff4e46aeafbc3beecf2のサイトをもとに検索機能を実装していましたが、エラーが出てしまいました。ほとんどサイトと同じようにしたのに出てしまったので、すごく困ってます。わかった方は、回答お願いします。(’_’)

error

1Started GET "/ugblogs/find" for ::1 at 2019-11-05 16:41:19 +0900 2Processing by UgblogsController#show as HTML 3 Parameters: {"id"=>"find"} 4 Ugblog Load (1.6ms) SELECT "ugblogs".* FROM "ugblogs" WHERE "ugblogs"."id" = ? LIMIT ? [["id", 0], ["LIMIT", 1]] 5 ↳ app/controllers/ugblogs_controller.rb:72 6Completed 404 Not Found in 122ms (ActiveRecord: 15.6ms) 7 8 9 10ActiveRecord::RecordNotFound (Couldn't find Ugblog with 'id'=find): 11 12app/controllers/ugblogs_controller.rb:72:in `set_ugblog'

controller

1class UgblogsController < ApplicationController 2 layout 'ugblogs' 3 before_action :set_ugblog, only: [:show, :edit, :update, :destroy] 4 5 # GET /ugblogs 6 # GET /ugblogs.json 7 def index 8 @ugblogs = Ugblog.all.order('created_at desc').limit(3) 9 end 10 11 # GET /ugblogs/1 12 # GET /ugblogs/1.json 13 def show 14 end 15 16 # GET /ugblogs/new 17 def new 18 @ugblog = Ugblog.new 19 end 20 21 # GET /ugblogs/1/edit 22 def edit 23 end 24 25 def search 26 @ugblogs = Ugblog.search(params[:search]) 27 end 28 29 # POST /ugblogs 30 # POST /ugblogs.json 31 def create 32 @ugblog = Ugblog.new(ugblog_params) 33 34 respond_to do |format| 35 if @ugblog.save 36 format.html { redirect_to @ugblog, notice: 'Ugblog was successfully created.' } 37 format.json { render :show, status: :created, location: @ugblog } 38 else 39 format.html { render :new } 40 format.json { render json: @ugblog.errors, status: :unprocessable_entity } 41 end 42 end 43 end 44 45 # PATCH/PUT /ugblogs/1 46 # PATCH/PUT /ugblogs/1.json 47 def update 48 respond_to do |format| 49 if @ugblog.update(ugblog_params) 50 format.html { redirect_to @ugblog, notice: 'Ugblog was successfully updated.' } 51 format.json { render :show, status: :ok, location: @ugblog } 52 else 53 format.html { render :edit } 54 format.json { render json: @ugblog.errors, status: :unprocessable_entity } 55 end 56 end 57 end 58 59 # DELETE /ugblogs/1 60 # DELETE /ugblogs/1.json 61 def destroy 62 @ugblog.destroy 63 respond_to do |format| 64 format.html { redirect_to ugblogs_url, notice: 'Ugblog was successfully destroyed.' } 65 format.json { head :no_content } 66 end 67 end 68 69 private 70 # Use callbacks to share common setup or constraints between actions. 71 def set_ugblog 72 73 @ugblog = Ugblog.find(params[:id]) 74 end 75 76 # Never trust parameters from the scary internet, only allow the white list through. 77 def ugblog_params 78 params.require(:ugblog).permit(:title, :subtitle, :Text, :img, :img_cache, :remove_img) 79 end 80end

model

1 mount_uploader :img, ImgUploader 2 3 validates :title, :subtitle, :Text, :img, presence: {message:'は、必須事項です。'} 4 def self.search(search) 5 return Ugblog.all unless search 6 Ugblog.where(['content LIKE ?', "%#{search}%"]) 7 end 8end

html

1<p>検索</p> 2<%= form_tag(search_path,:method => 'get') do %> 3 <%= text_field_tag :search %> 4 <%= submit_tag 'Search', :name => nil %> 5<% end %> 6 7<ul><% @ugblogs.each do |post| %> 8 <li> 9 <%= link_to ugblog.title, post %> 10 <%= ugblog.created_at %> 11 <%= ugblog.status %> 12 </li> 13 14 <% end %> 15</ul>

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

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

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

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

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

guest

回答2

0

/ugblogs/find" が  Parameters: {"id"=>"find"} と解釈されてるのが原因です config/routes.rb に定義が無いか間違えているのでしょう

投稿2019/11/05 08:08

winterboum

総合スコア23347

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

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

0

ベストアンサー

Parameters: {"id"=>"find"}

def set_ugblog @ugblog = Ugblog.find(params[:id]) end

findはidを検索キーとしてデータを取得するメソッドですが、
パラメータで渡しているのが"find"となっているのが原因かと。

投稿2019/11/05 08:03

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

yamada_yuuki

2019/11/05 08:44

回答ありがとうございます。初心者なので、意味はわかるのですが修正の仕方がわかりません。修正の仕方を詳しく教えていただけると嬉しいです。
退会済みユーザー

退会済みユーザー

2019/11/07 00:53

winterboumさんのコメントにもありますが、 config/routes.rb もしくは http://localhost:3000/rails/info/routes http://localhost:3000の部分は設定により違いますが自分のルートです。 を記載していただけますか?
yamada_yuuki

2019/11/07 08:39

root 'ugblogs#index'  になっています。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問