Error when deleting photo app in Ruby on Rails

Asked 2 years ago, Updated 2 years ago, 44 views

Assumptions/What you want to realize

I am currently creating a photo app on Ruby on Rails.
When I try to delete the photo, the following error message appears.
What should I do?

Problem/Error Message Occurring

https://gyazo.com/21705ed118a90aaf65dd127e348f4a2f

Source Code 1 "photos_controller.rb"

class PhotosController <ApplicationController
  before_action: login_check, only: [:new,:edit,:update,:destroy]

  before_action: set_current_user_photo, only: [:edit,:update,:destroy]

  # GET/photos
  # GET/photos.json
  def index
    @photos=Photo.all
  end

  # GET/photos/1
  # GET/photos/1.json
  def show
    @photo=Photo.includes(:user).find(params[:id])
    @[email protected](:user).all
    @[email protected](user_id:current_user.id)if current_user
  end

  # GET/photos/new
  def new
    @photo=current_user.photos.build
  end

  # GET/photos/1/edit
  default
  end

  # POST/photos
  # POST/photos.json
  def create
    @photo=Photo.new(photo_params)

    response_to do | format |
      [email protected]
        format.html {redirect_to @photo,notice: 'Photo was successfully created.'}
        format.json {render:show, status::created, location:@photo}
      else
        format.html {render:new}
        format.json {render json:@photo.errors, status::unprocessable_entity}
      end
    end
  end

  # PATCH/PUT/photos/1
  # PATCH/PUT/photos/1.json
  default update
    response_to do | format |
      [email protected](photo_params)
        format.html {redirect_to @photo,notice: 'Photo was successfully updated.'}
        format.json {render:show,status::ok,location:@photo}
      else
        format.html {render:edit}
        format.json {render json:@photo.errors, status::unprocessable_entity}
      end
    end
  end

  # DELETE/photos/1
  # DELETE/photos/1.json
  def destroy
    @photo.destroy
    response_to do | format |
      format.html {redirect_to photos_url, notice: 'Photo was successfully destroyed.'}
      format.json {head:no_content}
    end
  end

  private
  def login_check
    unless user_signed_in?
      flash [:alert] = "Please log in"
      redirect_to root_path
    end
  end

  # Use callbacks to share common setup or constraints between actions.
  default_current_user_photo
    @photo=current_user.photos.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def photo_params
    param.require(:photo).permit(:image,:caption,:user_id)
  end
end

Source Code 「 index.html.erb

<pid="notice">%=notice%>/p>

<h1>Listing Comments</h1>

<table>
  <thead>
  <tr>
    <th>User</th>
    <th>Photo</th>
    <th>Body</th>
    <th colspan="3"></th>
  </tr>
  </thead>

  <tbody>
  <%@comments.each do | comment | %>
    <tr>
      <td><%=comment.user_id%></td>
      <td><%=comment.photo_id%>/td>
      <td><%=comment.body%>/td>
      <td><%=link_to 'Show', comment %>/td>
      <td><%=link_to 'Edit', edit_comment_path(comment)%>/td>
      <td><%=link_to 'Destroy', comment, method::delete,data:{confirm:'Are you sure?'}%>/td>
    </tr>
  <%end%>
  </tbody>
</table>

<br>

<%=link_to 'New Comment', new_comment_path%>

ruby-on-rails ruby

2022-09-30 13:51

1 Answers

It seems that the relevant data does not exist, so why don't you first check if the corresponding data to be deleted exists in the photos table?To check, see

  • Check the GUI Viewer Tool (such as SequelPro)
  • rails console command to check via ActiveRecord
  • Check directly from the command line (mysql, psql, etc.)

and so on.

Another way to debug Rails is to use pry.http://ruby-rails.hatenadiary.com/entry/20141024/1414081224 This article is helpful for you to learn how to deploy and use pry.
For example, for this code,

def set_current_user_photo
  binding.ply
  @photo=current_user.photos.find(params[:id])
end

You may want to add binding.pry to the above location and see if you can get the current_user.photos and try to find out what caused it little by little.

Incidentally, the find method will cause ActiveRecord::RecordNotFound to occur if there is no exception, so if you want to do error handling, you can use find_by(id:params[:id]).

Note: Remove a single object
http://railsguides.jp/active_record_querying.html#%E5%8D%98%E4%B8%80%E3%81%AE%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E3%82%92%E5%8F%96%E3%82%8A%E5%87%BA%E3%81%99


2022-09-30 13:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.