User16119012
User16119012

Reputation: 957

creating a url for an action in rails

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.

Upvotes: 1

Views: 309

Answers (2)

jvnill
jvnill

Reputation: 29599

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)

Upvotes: 0

Ramiz Raja
Ramiz Raja

Reputation: 6030

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'

Upvotes: 1

Related Questions