1

I'm new to ROR, I'm trying to create an action from which I am able to delete a file from database. I have written a code for the same but giving error to url. View for the delete action:-

= 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'

Controller for the same:-

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

When I'm trying to run this code error is showing like invalid method Destroy_attachment path. Can any one help me to figure out the problem? Thanks in advance.

jvnill
  • 29,479
  • 4
  • 83
  • 86
User16119012
  • 957
  • 11
  • 25

2 Answers2

1

You can try this, it worked for me..

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'
Ramiz Raja
  • 5,942
  • 3
  • 27
  • 39
  • when i used above methods i got this confirmation message. This web page is being redirected to a new location. Would you like to resend the form data you have typed to the new location? and when i checked in responce it shows - Reload the page to get source for: http://0.0.0.0:3000/attachments/173. and ajaxified delete is not working to confirm file is deleted i have to reload the page. any suggestions. – User16119012 Feb 19 '13 at 06:18
  • There might be problem with javascript.. please see this post...http://stackoverflow.com/questions/4606860/rails-3-link-to-to-destroy-not-working – Ramiz Raja Feb 19 '13 at 07:00
0

your link_to should be

= 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')

or you can use the nested form

= 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

(take note of the indentations since you are most probably using haml, i just made it this way to read the code better)

jvnill
  • 29,479
  • 4
  • 83
  • 86
  • Thanks again it worked better.. can u pls tell me how u gave this path, mean what is the logic to give the path to the action.. it will help me next time – User16119012 Feb 19 '13 at 05:47
  • this will probably help you understand better http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing – jvnill Feb 19 '13 at 05:51