1

我是 ROR 的新手,我正在尝试创建一个操作,我可以从中删除数据库中的文件。我已经为相同的代码编写了一个代码,但对 url 给出了错误。查看删除操作:-

= link_to raw('<span>Delete</span>'), :method=> :delete,  destroy_attachment_path(attachment.descendants.last),
                              :data => { :confirm => 'Are you sure? This will permanently delete this file!' },
                              :remote => true,
                              :class => 'deleteShow deleteFile'

相同的控制器:-

enter code here

def destroy

  @attachment = Attachment.find(params[:id])

  @attachment.destroy

  respond_to do |format|
    format.html { redirect_to attachments_url }
    format.json { head :no_content }
  end
end

当我尝试运行此代码时,错误显示为无效方法 Destroy_attachment 路径。任何人都可以帮我解决问题吗?提前致谢。

4

2 回答 2

1

你可以试试这个,它对我有用..

link_to raw('<span>Delete</span>'), attachment.descendants.last, :method=> :delete,      :data => { :confirm => 'Are you sure? This will permanently delete this file!' },
                          :remote => true,
                          :class => 'deleteShow deleteFile'
于 2013-02-19T05:21:01.947 回答
0

你的 link_to 应该是

= link_to raw('<span>Delete</span>'), attachment_path(attachment.descendants.last),
  :method => :delete,
  :data => { :confirm => 'Are you sure? This will permanently delete this file!' },             
  :remote => true,
  :class => 'deleteShow deleteFile')

或者您可以使用嵌套形式

= link_to attachment_path(attachment.descendants.last),
  :method => :delete,
  :data => { :confirm => 'Are you sure? This will permanently delete this file!' },             
  :remote => true,
  :class => 'deleteShow deleteFile') do
    %span Delete

(请注意缩进,因为您很可能使用的是haml,我只是这样做是为了更好地阅读代码)

于 2013-02-19T05:22:51.963 回答