Problems with destroy


#1

Hello:

I have a destroy method in my controller - pretty standard:

def destroy
@goal = Goal.find(params[:id])
respond_to do |format|
format.html { redirect_to goals_url }
format.xml { head :ok }
end
end

And then I have a listing view which has the destroy method in the view

  • also pretty standard:

<%= link_to ‘Delete’, { :action => ‘destroy’, :id => goal }, :confirm =>
‘Are you sure?’, :post => true %>&nbsp|&nbsp

where the id = goal id - no problem retreiving that, becuase i see it in
my URL:

[myurl.com]/goals/13

but this is called, I get an error:

Unknown action

No action responded to 13

How can this be the case when the destroy method is correctly being
referenced in my view?

Thank you very much in advance for any help you can provide.

Mike


#2

The url ([myurl.com]/goals/13) looks like one of the new
“resource-based” urls from Rails 1.2, but it seems like the routing
system is expecting an old-style url ([myurl.com]/goals/destroy/13). If
you are using the new routing then it seems like you’ve somehow mixed up
the two systems. I don’t know much about the new system, but I know that
if you’re using it you should have a line like the following in your
routes.rb file:

map.resources :goals

Do you have this?

I you still can’t figure out the problem, please post your routes.rb
file - it might provide some more clues about what’s going wrong.


#3

Thanks so much for your reply. I think I do have a mix of routing
systems, but I’m not sure how it works. Does rails have a doc outlining
how routes in 1.2 works? here’s my config/routes.rb file:

ActionController::Routing::Routes.draw do |map|
map.resources :goals_tags

map.resources :tags

map.resources :goals

map.connect ‘’, :controller => “goals”

The priority is based upon order of creation: first created ->

highest priority.

Sample of regular route:

map.connect ‘products/:id’, :controller => ‘catalog’, :action =>

‘view’

Keep in mind you can assign values other than :controller and

:action

Sample of named route:

map.purchase ‘products/:id/purchase’, :controller => ‘catalog’,

:action => ‘purchase’

This route can be invoked with purchase_url(:id => product.id)

You can have the root of your site routed by hooking up ‘’

– just remember to delete public/index.html.

map.connect ‘’, :controller => “welcome”

Allow downloading Web Service WSDL as a file with an extension

instead of a file named ‘wsdl’

map.connect ‘:controller/service.wsdl’, :action => ‘wsdl’

Install the default route as the lowest priority.

map.connect ':controller/:action/:id.:format'

map.connect ‘:controller/:action/:id’

end

Thanks again - any help you can provide.


#4

<%= link_to ‘Delete’, { :action => ‘destroy’, :id => goal }, :confirm =>
‘Are you sure?’, :post => true %>&nbsp|&nbsp

I’m not sure if this is the problem, but could you try using the
following link in your view instead:

<%= link_to ‘Delete’, goal_path(goal), :confirm => ‘Are you sure?’,
:method => :delete %>

This appears to be the preferred way of writing links with the new
routing - it may be the only way. Don’t ask me how it works though - I
haven’t worked it out yet myself.

There does appear to be some “hidden” documentation for this in the
“resources.rb” file in the rails source. For some reason it’s marked
with a #:nodoc: tag, so it doesn’t appear in the generated
documentation. I don’t know whether this was just an oversight or
whether the documentation is not considered satisfactory.


#5

On Jan 12, 2007, at 10:17 PM, Mike D. wrote:

my URL:
referenced in my view?
I’m guessing you’re mapping ‘goals’ as a resource in your routes.rb
file? If so, you’ll need to specify the ‘delete’ method explicitly. Try:

<%= link_to ‘Delete’, { :action => ‘destroy’, :id => goal }, :confirm =>
‘Are you sure?’, :method => ‘delete’ %>

James.


#6

<…>

There does appear to be some “hidden” documentation for this in the
“resources.rb” file in the rails source. For some reason it’s marked
with a #:nodoc: tag, so it doesn’t appear in the generated
documentation. I don’t know whether this was just an oversight or
whether the documentation is not considered satisfactory.

You can try this:
http://caboo.se/doc/classes/ActionController/Resources.html

Regards,
Rimantas

http://rimantas.com/