rails - 创建有效的删除链接?

时间:2010-11-01 03:21:08

标签: ruby-on-rails ruby-on-rails-3

我有以下两种模式:

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

class Photo < ActiveRecord::Base
    belongs_to :photo_album

当用户在PhotoAlbum def SHOW页面上时,我希望有一个删除链接,所以我创建了以下内容:

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

其中包含以下内容:

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

但是这不起作用......在日志中,Rails试图删除带有“41”专辑构思的photo.ids,当我认为它应该删除时,photo.album_id = 41,然后是专辑。< / p>

您怎么看?

1 个答案:

答案 0 :(得分:1)

看起来你正在使用嵌套路线。您应该使用以下内容指定路径:

而是{p> project_photo_album_path(:project => @photoalbum.project, :photo_album => @photoalbum)。当您指定嵌套路由(例如/projects/:project_id/photo_albums/:photo_album_id)时,rails需要两个id,因为您只传入一个整数到路径方法,它似乎试图将该整数用作两个id。