# Map.resource and semi nested resources

**URL:** <https://www.ruby-forum.com/t/map-resource-and-semi-nested-resources/108211>\
**Category:** Rails\
**Created:** [June 30, 2007, 2:24am UTC](https://www.ruby-forum.com/t/map-resource-and-semi-nested-resources/108211 "2007-06-30T02:24:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave\_Warnock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/65b543/32.png) [@Dave\_Warnock](https://www.ruby-forum.com/u/Dave_Warnock)\
**Post date:** [June 30, 2007, 2:24am UTC](https://www.ruby-forum.com/t/map-resource-and-semi-nested-resources/108211/1 "2007-06-30T02:24:40Z")

</div>

Hi,

I have the following in routes.rb

map.resources :districts do |district|  
district.resources :circuits, :name\_prefix =\> ‘district\_’  
end

map.resources :circuits

I want full CRUD for circuits at /circuits but only partial CRUD at  
/districts/?/circuits (index and new). This is to avoid too many levels  
of nesting (there are several more to come).

I have got this fine for the UI (the links take you around exactly as I  
want). But the extra urls are still available.

1. How do I detect in the controller show if the url is  
/districts/1/circuits/1 which I want to redirect to the un-nested  
version at /circuits/1 ?

2. How do I know in index of controller whether I am  
/districts/1/circuits (limit to only district 1) or /circuits (list  
all)?

I guess it will be fairly obvious that I am pretty much a rails newbie.  
But I have looked in the Agile bk 2nd ed and as many articles on nested  
resources as I can find.

Thanks

Dave

---

<div class="post-metadata">

**Author:** ![Dave\_Warnock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/65b543/32.png) [@Dave\_Warnock](https://www.ruby-forum.com/u/Dave_Warnock)\
**Post date:** [June 30, 2007, 12:56pm UTC](https://www.ruby-forum.com/t/map-resource-and-semi-nested-resources/108211/2 "2007-06-30T12:56:17Z")

</div>

1. If the action was called through the nested route “/districts/1/  
circuits/1”, params[:district\_id] would be set. Otherwise it wouldn’t.

2. same here. look for params[:district].

If params[:district]:  
@circuits = Districts.find\_by\_id(params[:district\_id]).ciruits  
else  
@circuits = Circuit.find(:all)  
end

---

<div class="post-metadata">

**Author:** ![Dave\_Warnock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/65b543/32.png) [@Dave\_Warnock](https://www.ruby-forum.com/u/Dave_Warnock)\
**Post date:** [June 30, 2007, 2:45pm UTC](https://www.ruby-forum.com/t/map-resource-and-semi-nested-resources/108211/3 "2007-06-30T14:45:23Z")

</div>

The simplest thing you could do is check for the presence of params  
[:district\_id] in the circuits controller.

–  
Benjamin C.  
[http://www.bencurtis.com/](http://www.bencurtis.com/) – blog  
[http://agilewebdevelopment.com/rails-ecommerce](http://agilewebdevelopment.com/rails-ecommerce) – build e-commerce  
sites with Rails

---

<div class="post-metadata">

**Author:** ![Dave\_Warnock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/65b543/32.png) [@Dave\_Warnock](https://www.ruby-forum.com/u/Dave_Warnock)\
**Post date:** [July 1, 2007, 12:20am UTC](https://www.ruby-forum.com/t/map-resource-and-semi-nested-resources/108211/4 "2007-07-01T00:20:21Z")

</div>

Thanks Thorsten & Ben,

Now working as I want.

Dave
