How to delete nested resource?

szimek
Fri, 21 Mar 2008 10:12:33 -0000

Hi,

I got News model that has_one Asset. Asset belongs_to News.
AssetsController has only detroy method, asset is created in
NewsController create action. Both resources are in admin namespace.

Now I''d like to delete the asset. I tried something like this:

<%= link_to ''delete'', admin_news_item_asset_path(@news,
@news.asset), :confirm => ''Are you sure?'', :method =>
:delete %>

but I get the following error:

undefined method `has_key?'' for #<Asset:0xb6a22798>
vendor/rails/activerecord/lib/active_record/attribute_methods.rb:
240:in `method_missing''
vendor/rails/activerecord/lib/active_record/associations/
association_proxy.rb:128:in `send''
vendor/rails/activerecord/lib/active_record/associations/
association_proxy.rb:128:in `method_missing''
(eval):3:in `admin_news_item_asset_path''
...

If I use the following link:
<%= link_to ''delete'', admin_news_item_asset_path(@news),
:confirm =>
''Are you sure?'', :method => :delete %>
I don''t get any errors, but I don''t get asset id as well, only
news_item_id. I could use this to delete associated asset, but I''d
like to make it work as it should.

The routes are as follows:
map.namespace :admin do |admin|
  admin.resources :news, :singular => ''news_item'', :member
=>
{ :preview => :get } do |news_item|
    news_item.resource :asset
  end
end

I''m using rails 2.0.2.

Thanks in advance