在rails中为动作创建URL

时间:2013-02-19 05:05:52

标签: ruby-on-rails ruby

我是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路径。 任何人都可以帮我解决问题吗? 提前谢谢。

2 个答案:

答案 0 :(得分: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'

答案 1 :(得分: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,我只是这样做以更好地阅读代码)