开发者

rails - Creating a Delete Link that works?

I have the foll开发者_JAVA百科owing 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜