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:
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
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...
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.
> [...]
>
> > 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.