emersonthis
emersonthis

Reputation: 33408

link_to destroy action results in get request

I'm trying to make a link to delete a Project...

  <% @projects.each do |project| %>
    <h2><%= project.name %> (<%= project.videos.size %>)</h2> 
      <%= link_to("Delete!", {:controller=>'projects', :action=>'destroy', :id=>project.hashed_id}, {method: :delete} ) %>
  <% end %>

No matter what I do I get a get request so I end up on the projects/show view. Urrrgh!

My controller:

  def destroy
    @project = Project.where( :hashed_id=> params[:id]).first.destroy()
    flash[:notice]= "Project deleted"
    redirect_to(:action=>'index')
  end

What am I missing?

PS: I found this, but the solution doesn't work for me.

Update

$ rake routes
      Prefix Verb   URI Pattern                  Controller#Action
        root GET    /                            pages#home
    projects GET    /projects(.:format)          projects#index
             POST   /projects(.:format)          projects#create
 new_project GET    /projects/new(.:format)      projects#new
edit_project GET    /projects/:id/edit(.:format) projects#edit
     project GET    /projects/:id(.:format)      projects#show
             PATCH  /projects/:id(.:format)      projects#update
             PUT    /projects/:id(.:format)      projects#update
             DELETE /projects/:id(.:format)      projects#destroy

Update 2 Here's the error I get:

enter image description here

Upvotes: 0

Views: 4028

Answers (3)

Try this:

<%= link_to 'Destroy', project_path(project), method: :delete %>

It should work. If doesn't, paste the server log in comments or try to check your generated path.

Or check at your rails c:

app.project_path(Project.first)

Good luck!

Upvotes: 0

Benjamin Tan Wei Hao
Benjamin Tan Wei Hao

Reputation: 9701

Paste your output of rake routes. I suspect that the routes are not matching, that's why it falls to #show instead.

Make sure you have jquery-rails installed too.

See Delete / Destroy is not working in rails 3 with jQuery.

In general, make sure that you have included all the necessary javascript files.

Upvotes: 1

Sami
Sami

Reputation: 1051

Do something like this:

Controller:

def destroy
  Flower.find(params[:id]).destroy
  redirect_to :action => 'index'
end

Views:

<%= link_to 'Destroy', flower_path(flower), method: :delete, data: { confirm: 'Are you sure?' } %>

Upvotes: 0

Related Questions