前提・実現したいこと
ruby,sinatra,ActiveRecordで簡単なQ&Aアプリを作成しています。
画面遷移やDBアクセス自体は正常にできているようにできるのですが、コンソール上にActiveRecord::RecordNotFound
エラーが発生します。
どの/
以外のどのリクエストにも同じエラーメッセージが出ます。
以下のSQLが必ず発行されていて、そこでSQLエラーになっているようです。
Question Load (0.5ms) SELECT "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ? [["id", 0], ["LIMIT", 1]]
エラーがでなくなるような解決策を教えていただきたいです。
発生している問題・エラーメッセージ
mb:test kawac$ ruby hello.rb /Users/kawac/.rvm/gems/ruby-2.6.1/gems/activesupport-5.0.0/lib/active_support/xml_mini.rb:51: warning: constant ::Fixnum is deprecated /Users/kawac/.rvm/gems/ruby-2.6.1/gems/activesupport-5.0.0/lib/active_support/xml_mini.rb:52: warning: constant ::Bignum is deprecated [2019-09-26 21:16:41] INFO WEBrick 1.4.2 [2019-09-26 21:16:41] INFO ruby 2.6.1 (2019-01-30) [x86_64-darwin18] == Sinatra (v2.0.7) has taken the stage on 4567 for development with backup from WEBrick [2019-09-26 21:16:41] INFO WEBrick::HTTPServer#start: pid=5210 port=4567 D, [2019-09-26T21:16:50.168878 #5210] DEBUG -- : Question Load (1.8ms) SELECT "questions".* FROM "questions" ::1 - - [26/Sep/2019:21:16:50 +0900] "GET / HTTP/1.1" 200 3264 0.0510 ::1 - - [26/Sep/2019:21:16:50 JST] "GET / HTTP/1.1" 200 3264 - -> / D, [2019-09-26T21:16:50.363144 #5210] DEBUG -- : Question Load (0.5ms) SELECT "questions".* FROM "questions" WHERE "questions"."id" = ? LIMIT ? [["id", 0], ["LIMIT", 1]] 2019-09-26 21:16:50 - ActiveRecord::RecordNotFound - Couldn't find Question with 'id'=favicon.ico: /Users/kawac/.rvm/gems/ruby-2.6.1/gems/activerecord-5.0.0/lib/active_record/core.rb:173:in `find' hello.rb:35:in `block in <main>' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:1635:in `call' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:1635:in `block in compile!' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:987:in `block (3 levels) in route!' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:1006:in `route_eval' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:987:in `block (2 levels) in route!' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:1035:in `block in process_route' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:1033:in `catch' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:1033:in `process_route' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:985:in `block in route!' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:984:in `each' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:984:in `route!' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:1097:in `block in dispatch!' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:1071:in `block in invoke' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:1071:in `catch' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:1071:in `invoke' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:1094:in `dispatch!' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:919:in `block in call!' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:1071:in `block in invoke' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:1071:in `catch' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:1071:in `invoke' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:919:in `call!' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:908:in `call' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/rack-protection-2.0.7/lib/rack/protection/xss_header.rb:18:in `call' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/rack-protection-2.0.7/lib/rack/protection/path_traversal.rb:16:in `call' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/rack-protection-2.0.7/lib/rack/protection/json_csrf.rb:26:in `call' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/rack-protection-2.0.7/lib/rack/protection/base.rb:50:in `call' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/rack-protection-2.0.7/lib/rack/protection/base.rb:50:in `call' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/rack-protection-2.0.7/lib/rack/protection/frame_options.rb:31:in `call' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/rack-2.0.7/lib/rack/logger.rb:15:in `call' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/rack-2.0.7/lib/rack/common_logger.rb:33:in `call' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:231:in `call' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:224:in `call' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/rack-2.0.7/lib/rack/head.rb:12:in `call' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/rack-2.0.7/lib/rack/method_override.rb:22:in `call' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/show_exceptions.rb:22:in `call' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:194:in `call' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:1950:in `call' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:1502:in `block in call' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:1729:in `synchronize' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/sinatra-2.0.7/lib/sinatra/base.rb:1502:in `call' /Users/kawac/.rvm/gems/ruby-2.6.1/gems/rack-2.0.7/lib/rack/handler/webrick.rb:86:in `service' /Users/kawac/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/webrick/httpserver.rb:140:in `service' /Users/kawac/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/webrick/httpserver.rb:96:in `run' /Users/kawac/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/webrick/server.rb:307:in `block in start_thread' ::1 - - [26/Sep/2019:21:16:50 JST] "GET /favicon.ico HTTP/1.1" 500 133115 http://localhost:4567/ -> /favicon.ico
該当のソースコード
Ruby
1# models 2class Question < ActiveRecord::Base 3 has_many :answers, dependent: :destroy 4end 5 6class Answer < ActiveRecord::Base 7 belongs_to :question 8end 9 10get '/test' do 11 puts "hello test" 12end 13# question 14get '/' do 15 @title = "My Q&A" 16 @questions = Question.all 17 erb :index 18end 19 20(略) 21
erb
1<h1>My question</h1> 2<div class="row"> 3 <div class="col-md-12"> 4 <table class="table table-striped"> 5 <thead class="thead-light"> 6 <th>ID</th> 7 <th>name</th> 8 <th>Question</th> 9 <th>Action</th> 10 </thead> 11 <tbody> 12 <% @questions.each do |q| %> 13 <tr> 14 <td><%= q.id %></td> 15 <td><%= q.name %></td> 16 <td> 17 <a href="/question/<%= q.id %>"><%= q.content %></a> 18 </td> 19 <td> 20 <form action="/<%= q.id %>" method="get"> 21 <input type="submit" value="edit"> 22 </form> 23 <form action="destroy" method="post"> 24 <input type="hidden" name="id" value="<%= q.id %>%"> 25 <input type="submit" value="delete"> 26 </form> 27 </td> 28 </tr> 29 <% end %> 30 </tbody> 31 </table> 32 </div> 33</div> 34 35<%# quesiton追加 %> 36<form action="/create" method="post"> 37 <input type="text" name="content"> 38 <input type="hidden" name="name" value="Alice"> 39 <input type="submit" value="Post Question"> 40</form>
試したこと
ActiveRecordやsqliteのgemのバージョンを下げても状況変わらずでした。
補足情報(FW/ツールのバージョンなど)
RubyやSinatra,DBのバージョンはいかになります。
mb:test kawac$ ruby -v ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin18] mb:test kawac$ sqlite3 -version 3.24.0 2018-06-04 14:10:15 95fbac39baaab1c3a84fdfc82ccb7f42398b2e92f18a2a57bce1d4a713cbaapl mb:test kawac$ gem list|grep activerecord activerecord (5.0.0) mb:test kawac$ gem list|grep sqlite sqlite3 (1.3.13) mb:test kawac$ gem list|grep sinatra sinatra (2.0.7) sinatra-contrib (2.0.7)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/09/27 10:02 編集