Frederick C. wrote:
Couldn’t find [Record] with ID=X
[Record] is the name of my model.
I always check that the record actually exists in my database, before I
press the “delete” link.
What is wrong here? Any ideas?
We’re not psychic.
I don’t expect you to be, either.
But I have been running through all my other models to check if
something could cause the problem, but there’s nothing.
(but if I had to take a guess it would be that the
parameters your controller receives aren’t the ones you thought you’d
get).
Yes, that’s what I thought too. But I’ve also checked if the record’s id
matches the id in params[:id].
Actually I thought, this was a comon problem. I see it’s not. Therefore
I’ll give you some code snippets, that might give a hint.
def destroy
# Finding the current user’s root folder.
@root = Folder.find(:first, :conditions => "folder_id is null and
user_id = "+current_user.id.to_s)
# Finding the upload to get destroyed. Error at following line:
@upload = Upload.find(params[:id])
# Deleting the upload on the harddrive.
File.delete @upload.public_filename
# Destroys the upload.
@upload.destroy
# If the file has been deleted from the user's root folder.
if @root.id == @upload.folder_id
# ... redirect to the home page.
url = {}
else
# ... else redirect to the folder's parent folder.
url = {:controller => "folders", :action => "show", :id =>
@upload.folder_id}
end
flash[:notice] = “…”
# Redirect goes here.
redirect_to url
end
According to my oppinion, there should be nothing wrong here. But what
about the model?
has_attachment :storage => :file_system, :path_prefix => table_name
belongs_to :folder
validates_uniqueness_of :filename, :scope => :folder_id
validates_presence_of :filename
belongs_to :user, :foreign_key => “user_id”
attr_accessor :email
Nothing unusual, right? This is the folder model. Each folder contains
both folders and uploads:
acts_as_tree :order => “name”, :foreign_key => ‘folder_id’
belongs_to :parent, :foreign_key => ‘folder_id’, :class_name =>
‘Folder’
has_many :children, :foreign_key => ‘folder_id’, :class_name =>
‘Folder’, :dependent => :destroy
has_many :uploads, :dependent => :destroy
belongs_to :user
validates_uniqueness_of :name, :scope => :folder_id
Could anything in these code snippets cause the problem?