1

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 
Rails beginner
  • 14,321
  • 35
  • 137
  • 257

4 Answers4

6

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

<%= link_to 'Destroy', [:admin, konkurrancer], :method => :delete %>
Kris
  • 2,108
  • 18
  • 19
  • My route is: namespace :admin do resources :konkurrancers do collection do delete :delete_multiple end end – Rails beginner May 26 '11 at 09:00
  • @Rails beginner: what kind of error are you getting? Since your route is namespaced, have you tried this: `<%= link_to 'Destroy', [:admin, konkurrancer], :method => :delete %>`? – Kris May 26 '11 at 10:39
  • I get the error: undefined method `konkurrancer_path'. I have updated my question with your 2nd solution – Rails beginner May 26 '11 at 10:52
  • @Rails beginner: can you provide us with the output of `rake routes`? – Kris May 27 '11 at 11:46
  • I have now posted the rake routes – Rails beginner May 30 '11 at 18:04
  • @Rails beginner: I have started to run out of ideas. What I did notice is that the error is about not finding the `show` action, while you're trying to add a destroy link. Are you sure the error occurs at the position in your view that you showed us? – Kris May 31 '11 at 10:52
  • Nobody that have an answer to this problem ? Is there more information I can provide to solve this problem? – Rails beginner Jun 02 '11 at 12:04
  • The error message you added to your question shows source lines that don't correspond to the code you're giving in your question. Please update your source code according to the answer I gave and give us the new error message. As a side note: I believe there is nothing wrong with the ID... – Kris Jun 03 '11 at 13:47
2

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 %>
Spyros
  • 46,820
  • 25
  • 86
  • 129
  • I have tried both solutions - They are not working. The first renders the show page and the 2nd renders a route error – Rails beginner May 31 '11 at 15:59
  • You do not have correct routes. You need something like "scope :path => '/your_path', :controller => :your_controller do delete 'your_url' => :destroy, :as => 'destroy_konkurrancer' end" in your routes.rb for the second one to work(fix your_ appropriately). – Spyros May 31 '11 at 16:23
  • Nobody that have an answer to this problem ? Is there more information I can provide to solve this problem? – Rails beginner Jun 02 '11 at 12:04
  • I've given you the answer. You're probably not doing something correctly and keep getting the error. If you don't understand how the route that i specify works, take a look here : http://guides.rubyonrails.org/routing.html – Spyros Jun 02 '11 at 14:57
0

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.

nessur
  • 1,143
  • 1
  • 11
  • 18
0

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.

LPL
  • 16,827
  • 6
  • 51
  • 95
Chiranjib
  • 21
  • 1
  • 4