Rails beginner
Rails beginner

Reputation: 14514

Rails 3 destroy link not working

My destroy link is not working.

My index view:

<div id="konkurrancer"><%= render 'konkurrencer', :remote => true %></div><%= debug(params) %>

My konkurrencer partial:

<% for konkurrancer in @konkurrancers %>    <%= link_to 'Destroy', [:admin, konkurrancer], :method => :delete %> <% end %>

And I get this error:

No route matches {:action=>"show", :controller=>"admin/konkurrancers", :id=>#<Konkurrancer id: 41, name: " Vind 16.000 kr., til Bilka, Føtex eller Netto

EDIT ERROR IN VIEW:

ActionController::RoutingError in Admin/konkurrancers#index

Showing C:/Rails/konkurranceportalen/app/views/admin/konkurrancers/_konkurrencer.html.erb where line #59 raised:

No route matches {:action=>"show", :controller=>"admin/konkurrancers", :id=>#<Konkurrancer id: 41, name: " Vind 16.000 kr., til Bilka, Føtex eller Netto", banner1: "http://partner.smartresponse-media.com/42/1092/1210...", banner2: "http://partner.smartresponse-media.com/42/1092/1210...", vaerdi: 16000, note: "", udtraekkes: "2011-05-31 22:00:00", created_at: "2011-05-04 12:35:44", updated_at: "2011-05-05 14:55:35", cached_slug: "vind-16000-kr-til-bilka-f\xC3\xB8tex-eller-netto", tid: "4 min", form: "Quiz", rating_score: 5, ratings: 1, rating: 5, photo_file_name: nil, photo_content_type: nil, photo_file_size: nil, photo_updated_at: nil, photo2_file_name: nil, photo2_content_type: nil, photo2_file_size: nil, photo2_updated_at: nil, image_remote_url: "", image_remote_url_2: "">}

Extracted source (around line #59):

56:       <td><%= konkurrancer.form %></td>
57:        <td><%= link_to 'Vis', admin_konkurrancer_path(konkurrancer.id) %></td>
58:     <td><%= link_to 'Redigere', {:action => 'edit', :id => konkurrancer.id}, :class => 'action edit' %></td>
59:     <td> <%= link_to 'Destroy', admin_konkurrancer_path(konkurrancer), :method => :delete %> </td>
60: 
61: 
62:     </td>

Rake routes:

delete_multiple_admin_konkurrancers DELETE /admin/konkurrancers/delete_multiple(
.:format) {:action=>"delete_multiple", :controller=>"admin/konkurrancers"}
                admin_konkurrancers GET    /admin/konkurrancers(.:format)
          {:action=>"index", :controller=>"admin/konkurrancers"}
                                    POST   /admin/konkurrancers(.:format)
          {:action=>"create", :controller=>"admin/konkurrancers"}
             new_admin_konkurrancer GET    /admin/konkurrancers/new(.:format)
          {:action=>"new", :controller=>"admin/konkurrancers"}
            edit_admin_konkurrancer GET    /admin/konkurrancers/:id/edit(.:forma
t)        {:action=>"edit", :controller=>"admin/konkurrancers"}
                 admin_konkurrancer GET    /admin/konkurrancers/:id(.:format)
          {:action=>"show", :controller=>"admin/konkurrancers"}
                                    PUT    /admin/konkurrancers/:id(.:format)
          {:action=>"update", :controller=>"admin/konkurrancers"}
                                    DELETE /admin/konkurrancers/:id(.:format)
          {:action=>"destroy", :controller=>"admin/konkurrancers"}

My route file:

namespace :admin do
resources :tags
resources :kategoris
 resources :konkurrancers do
      collection do
        delete :delete_multiple
      end
    end
resources :reklamers
   end 

Upvotes: 1

Views: 3504

Answers (4)

Chiranjib
Chiranjib

Reputation: 21

In case of RESTful resources, try this:

{<%= link_to 'Destroy', [:admin, konkurrancer], :method => :delete %>}

and use appropriate javascript helper.

This is problem of javascript-helper of rails. You need to use rails.js appropriately. If you use prototype.js then in rails.js you need prototype helpers methods else if you use jquery then you need rails jquery helper.

Upvotes: 0

nessur
nessur

Reputation: 1143

When working with namespaced controllers and routes, you have to use namespaced models in order for the link_to helpers to function properly.

When I write extra admin pages for models, I also generate some wrapper models in the admin namespace.

e.g., in app/models/admin/person.rb

class Admin::Person < Person    
end

Makes forms of all sorts, and links, much more simple.

Upvotes: 0

Kris
Kris

Reputation: 2128

In case you're using RESTful resources, try this:

<%= link_to 'Destroy', [:admin, konkurrancer], :method => :delete %>

Upvotes: 6

Spyros
Spyros

Reputation: 48706

You probably need to change it to something like:

<%= link_to 'Destroy', :action => 'destroy', :id => konkurrancer.id, :method => :delete %>

or if you use RESTful routes, something like :

<%= link_to 'Destroy', delete_konkurrancer(:id => konkurrancer.id), :method => :delete %>

Upvotes: 2

Related Questions