AnApprentice
AnApprentice

Reputation: 111070

rails - Creating a Delete Link that works?

I have the following two models:

class PhotoAlbum < ActiveRecord::Base
    belongs_to :project
    has_many :photos, :dependent => :destroy

class Photo < ActiveRecord::Base
    belongs_to :photo_album

When the user is on the PhotoAlbum def SHOW page, I want there to be a delete link, so I created the following:

<%= link_to "Delete", project_photo_album_path(@photoalbum.id), :confirm => "Are you sure?", :method=>:delete %>

Which renders the following:

<a href="/projects/41/photo_albums/41" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Delete</a>

But this does not work... In the logs, Rails is trying to delete photo.ids with "41" the album idea, when I think it should be deleting, photo.album_id = 41, then the album.

What do you think?

Upvotes: 2

Views: 319

Answers (1)

Jeremy
Jeremy

Reputation: 4930

It looks like you're using nested routes. You should specify the path with something like:

project_photo_album_path(:project => @photoalbum.project, :photo_album => @photoalbum) instead. When you specify a nested route such as /projects/:project_id/photo_albums/:photo_album_id, rails needs both id's, since you've passed in only one integer to the path method, it seems to be trying to use that integer as both id's.

Upvotes: 1

Related Questions