Forum: Ruby on Rails Deleting images with attachment_fu

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D0e3f7bbe7a18f5ea1d58ff70ba22352?d=identicon&s=25 Richard Yoo (flychunjae)
on 2009-03-06 11:44
Hi

I'm trying to delete a user's profile image that was uploaded with
attachment_fu directly to storage, without going to the view of the
Photo controller.

Basically the model is set up so

user has many photos
photos belong to user

    @user = User.find(params[:id])
    @photo = Photo.new(params[:photo])

there is no problem viewing the photos but i'm trying to have a link
next to the picture in the view of the User controller which would
delete the photo.

i tried:

def destroyphoto
    @user = User.find(params[:id])
    @photo = @user.photos.find(params[:id])
    @photo.destroy
end

with:

<%= link_to "delete", destroyphoto_user_url, :method => :delete %>

but i get the:

Couldn't find Photo with ID=1 AND (`photos`.user_id = 1)

the photo's id isn't 1 and it's actually many because of the original
image and the thumbnails.

any ideas?
D188e591eac11021329b8821a5f954c7?d=identicon&s=25 Ar Chron (railsdog)
on 2009-03-06 15:12
> any ideas?

You shouldn't be passing in params[:id] in your photos find...
params[:id] is your users_id, not a photo_id.

have you tried just

@photos = @user.photos

Then you'll have to iterate across all the photos returned to delete
each one.
This topic is locked and can not be replied to.