Rubyのエラーを直したいです。
プログラミング初学者です。
Ruby 5.2.1を使用してブログアプリを作成しており、削除機能を実装中に以下の
エラーメッセージが発生したため、エラーを回避したい。
発生している問題・エラーメッセージ
NoMethodError in BlogsController#destroy undefined method `destroy' for nil:NilClass
Route.rb
def destroy
@Blog = Blog.find_by(id: params[:id])
@blog.destroy
redirect_to root_path, notice: '削除に成功しました'
else
redirect_to root_path
end
end
def destroy @Blog = Blog.find_by(id: params[:id]) @blog.destroy redirect_to root_path, notice: '削除に成功しました' else redirect_to root_path end end private def find_blog @blog = Blog.find(params[:id]) end def blog_params params.permit(:title,:text) end
destroy のHTML
<div class="contents row"> <div class="success"> <h3> 削除が完了しました。 </h3> <a class="btn" href="/">投稿一覧へ戻る</a> </div> </div>処理をした際のlog
↳ /home/ec2-user/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
(0.2ms) SELECT schema_migrations
.version
FROM schema_migrations
ORDER BY schema_migrations
.version
ASC
↳ /home/ec2-user/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
/home/ec2-user/environment/blog2/app/controllers/blogs_controller.rb:35: warning: else without rescue is useless
Processing by BlogsController#destroy as HTML
Parameters: {"authenticity_token"=>"o1HHz2cavK9DwfiGyHqJqTKeHuMIdUdx/ZF59McYM4QvMbtKwF/SSxoA4SWeIs5oXztcYlXtodV3Q1oWeu4ayA==", "id"=>":user_id"}
Blog Load (0.2ms) SELECT blogs
.* FROM blogs
WHERE blogs
.id
= 0 LIMIT 1
↳ app/controllers/blogs_controller.rb:30
Completed 500 Internal Server Error in 13ms (ActiveRecord: 0.7ms)
NoMethodError (undefined method `destroy' for nil:NilClass):
app/controllers/blogs_controller.rb:31:in `destroy'
index.html
<% @blogs.each do |blog| %>
<div class="kakomi-box1"> <p class="title"><%= blog.title %></p> <p class="text"><%= blog.text %></p> <p class="date"><%= blog.created_at.try(:strftime,"%Y年%m月%d日") %></p> <p class="id"><%= blog.user_id %>の投稿です。</p> <% if user_signed_in? %> <%= link_to '削除', "/blogs/:id/destroy",class:'cp_btn2', method: :delete %> <a href="#" class="cp_btn1">編集</a> <% else %> <a href="#" class="cp_btn2">詳細</a> <% end %> </div> <% end %> ### routes.rbを追記 ''' Rails.application.routes.draw do devise_for :users root 'blogs#index' get 'blogs' => 'blogs#index' get 'blogs/new' => 'blogs#new' post 'blogs' => 'blogs#create' delete '/blogs/:id' => 'blogs#destroy' get 'blogs/:id/edit' => 'blogs#edit' end '''<%= link_to '削除', blog_path(blog),class:'cp_btn2', method: :delete %>の結果の処理
Started DELETE "/blogs.30" for 126.165.251.179 at 2019-11-19 15:38:13 +0000
Cannot render console from 126.165.251.179! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
(0.3ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
↳ /home/ec2-user/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
(0.2ms) SELECT schema_migrations
.version
FROM schema_migrations
ORDER BY schema_migrations
.version
ASC
↳ /home/ec2-user/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
index.html.erbの修正後
<% @blogs.each do |blog| %>
<div class="kakomi-box1"> <p class="title"><%= blog.title %></p> <p class="text"><%= blog.text %></p> <p class="date"><%= blog.created_at.try(:strftime,"%Y年%m月%d日") %></p> <p class="id"><%= blog.user_id %>の投稿です。</p> <% if user_signed_in? && current_user.id == blog.user_id %> <%= link_to '削除', blog_path(blog),class:'cp_btn2', method: :delete %> <a href="#" class="cp_btn1">編集</a> <a href="#" class="cp_btn2">詳細</a> <% else %> <a href="#" class="cp_btn2">コメント</a> <% end %> </div> <% end %>