How to delete nested resource?

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

not 100% sure, but: admin_news_item_asset_path(@news, @news.asset)

should be something like: admin_news_item_asset_path(@news, :asset_id => @news.asset)

since rails can guess the param name (:id) only for those parts of the path, that are declared in the routes, all others must be named in hash fashion

but i'm not sure, if you need that here. why hand over the asset.id, if it's stored in @news anyway you could simply call the thing for @news and in destroy use @news.asset

or in the has_one declaration simply add: has_one :asset, :dependent => destroy and you'll never have to think about it again...

Thanks.

It works, but I still think that rails should automatically figure out ID of the second parameter passed.

If I run "rake routes", I can see "admin_news_item_asset_path" listed there together with DELETE method... The generated url is "/admin/news/ 23/asset?id=35", but it should be enough for rails to figure out ID of the asset even with url like this "/admin/news/23/asset".

[...]

The generated url is "/admin/news/ 23/asset?id=35", but it should be enough for rails to figure out ID of the asset even with url like this "/admin/news/23/asset".

  Only if news has_one asset. Otherwise, Rails can't figure out which of N assets you want to delete.

  Sorry my poor English.

  Best regards,

Thanks, but I have specified the relationship: News has_one :asset, Asset belongs_to :news.

BTW. I'm using edge rails 67328 and not 2.0.2 version.

> [...] > > > The generated url is "/admin/news/ > > 23/asset?id=35", but it should be enough for rails to figure out ID of > > the asset even with url like this "/admin/news/23/asset". > > Only if news has_one asset. Otherwise, Rails can't figure out > which of N assets you want to delete.

[...]

Thanks, but I have specified the relationship: News has_one :asset, Asset belongs_to :news.

  Sorry. I missed the point here. You're right.

  Best regards,