前提・実現したいこと
railsでアプリを作っているのですが、投稿を削除ボタンを押すとエラーが出てしまいます。
エラーが出るのですが一度サーバーを止め更新すると投稿は消えています。
ぜひご教授くださいませ!!
発生している問題・エラーメッセージ
エラーメッセージ Started DELETE "/posts/33" for 180.27.220.102 at 2021-01-16 04:00:16 +0000 Cannot render console from 180.27.220.102! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by PostsController#destroy as HTML Parameters: {"authenticity_token"=>"pPME+CgkncdykxKzdX1zo7VNNaT5l3sMbIIozeSladLX0b+5cYyP0ZdZ6wIGUyfwbUdS78+nmwrky14PTAk5fA==", "id"=>"33"} User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1 ↳ /home/ec2-user/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.4.4/lib/active_record/log_subscriber.rb:98 Post Load (0.8ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 33 LIMIT 1 ↳ app/controllers/posts_controller.rb:59 Post Load (1.4ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`user_id` = 1 AND `posts`.`id` = 33 LIMIT 1 ↳ app/controllers/posts_controller.rb:64 (0.7ms) BEGIN ↳ app/controllers/posts_controller.rb:45 Favorite Load (1.2ms) SELECT `favorites`.* FROM `favorites` WHERE `favorites`.`post_id` = 33 ↳ app/controllers/posts_controller.rb:45 Post Destroy (0.8ms) DELETE FROM `posts` WHERE `posts`.`id` = 33 ↳ app/controllers/posts_controller.rb:45 (2.1ms) COMMIT ↳ app/controllers/posts_controller.rb:45 Redirected to https://26ae0694317e457d88cf5435b910a644.vfs.cloud9.us-east-1.amazonaws.com/posts/33 Completed 302 Found in 16ms (ActiveRecord: 7.3ms) Started GET "/posts/33" for 180.27.220.102 at 2021-01-16 04:00:16 +0000 Cannot render console from 180.27.220.102! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by PostsController#show as HTML Parameters: {"id"=>"33"} Post Load (0.6ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 33 LIMIT 1 ↳ app/controllers/posts_controller.rb:59 Completed 404 Not Found in 3ms (ActiveRecord: 0.6ms) ActiveRecord::RecordNotFound (Couldn't find Post with 'id'=33): app/controllers/posts_controller.rb:59:in `find_post'
該当のソースコード
posts/controller class PostsController < ApplicationController before_action :authenticate_user!, only: [:new,:create,:edit,:update,:destroy] before_action :find_post, only: [:show, :edit, :update, :destroy] before_action :correct_user, only: [:destroy,:edit] def index @posts = Post.order(id: :desc).page(params[:page]).per(8) end def show end def new @post = Post.new end def create @post = current_user.posts.build(post_params) if @post.save flash[:success] = "正常に投稿されました!" redirect_to posts_path else flash.now[:danger] = "正常に投稿できませんでした。" render :new end end def edit end def update if @post.update(post_params) flash[:success] = 'ノートは正常に更新されました' redirect_to root_path else flash.now[:danger] = 'ノートは更新されませんでした' render :edit end end def destroy @post.destroy flash[:success] = 'メッセージを削除しました。' redirect_back(fallback_location: root_path) end private def post_params params.require(:post).permit(:content,:title,:teacher_name,:subject,:post_image) end def find_post @post = Post.find(params[:id]) end def correct_user @post = current_user.posts.find_by(id: params[:id]) unless @post redirect_to new_user_session_path end end end
posts/show <div class="card md-3 style="max-width: 600px;""> <div class="row no-gutters-1"> <div class="col-lg-5"> <%= attachment_image_tag @post, :post_image, class: "card-img-top-1"%> </div> <div class="col-lg-7"> <div class="card-body-1"> <div class="form-groupp"> <label class="label">商品名</label> <h4 class="card-title-1"><%=@post.title%></h4> </div> <div class="form-groupp"> <label class="label">教科名</label> <h6 class="card-subtitle-1"><%=@post.subject%></h6> </div> <div class="form-groupp"> <label class="label">先生の名前</label> <h6 class="card-subtitle-1"><%=@post.teacher_name%></h6> </div> <div class="form-groupt"> <label class="label">説明</label> <p class="card-text-5"><%=@post.content%></p> </div> <div class="text-center-1"> <% if current_user == @post.user %> <%= link_to "削除", @post, method: :delete, data: { confirm: "You sure?" }, class: 'bt' %> <%= link_to "編集", edit_post_path(@post), class: "bta"%> <%end%> <div id="favorites_buttons_<%= @post.id %>"> <%= render partial: 'favorites/favorite_button', locals: { post: @post} %> </div> </div> </div> </div> </div> </div>
あなたの回答
tips
プレビュー