# Simple problem with Resources Routes

**URL:** <https://www.ruby-forum.com/t/simple-problem-with-resources-routes/103297>\
**Category:** Rails\
**Created:** [May 3, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/simple-problem-with-resources-routes/103297 "2007-05-03T23:19:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [May 3, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/simple-problem-with-resources-routes/103297/1 "2007-05-03T23:19:10Z")

</div>

Ok, I’m quite near to grasp all the resources and related routing  
affair…

There is, anyway, a problem that I’m not able to solve: I do not get the  
picture, here.

I have Offers, and I have Categories… (offers are categorized)

Offers could be listed and shown alone, so you have: /offers/23 or  
/offers  
and even /offers.rss

The problem comes when I try to get the Offers related to some Category

The REST way is /categories/32/offers but it won’t work… I can’'t (or  
I  
don’t know  
how) use offers\_url … or many other url helper that worked without  
nesting…

Here it is my current (not working) routes.rb

map.connect ‘’, :controller =\> ‘offers’

map.resources :offers  
map.resources :offers, :new =\> { :new =\> :any, :preview =\> :post }

map.resources :categories do |categories|  
categories.resources :offers  
end

–  
Claudio C.

---

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [May 3, 2007, 11:22pm UTC](https://www.ruby-forum.com/t/simple-problem-with-resources-routes/103297/2 "2007-05-03T23:22:11Z")

</div>

You need a name prefix if you’re specifying resources multiple times. A  
suggestion for your situation:

map.connect ‘’, :controller =\> ‘offers’

map.resources :offers, :new =\> { :new =\> :any, :preview =\> :post },  
:name\_prefix =\> ‘all\_’

map.resources :categories do |categories|  
categories.resources :offers  
end

This gives you all\_offers\_path and categories\_offers\_path along with the  
URLs of “/offers” and “/categories/12/offers” and all related  
variations.

Jason

---

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [May 4, 2007, 12:03am UTC](https://www.ruby-forum.com/t/simple-problem-with-resources-routes/103297/3 "2007-05-04T00:03:15Z")

</div>

Claudio,

The problem is that the routes conflict. The way to get the routes  
untangled is to use :name\_prefix =\> ‘something\_’ and  
possibly :path\_prefix =\> ‘/something/:some\_id’. I wanted to  
experiment with this to see what all is necessary, because I’ve been  
facing this in my own application.

The trick, once I got my routes untangled, was to make sure the views  
and the controller stays untangled too. If I were in your  
application, I’d probably create a:

before\_filter :find\_category

and then

def find\_category  
@category = nil  
@category = Category.find(params[:category\_id]) if  
params[:category\_id]  
end

Then, when you call all the orders\_url and order\_url(@order), etc. in  
your controller and views, you’ll put a conditional there, to pass  
@category if @category (like @category ? order\_url(@category,  
@order) : order\_url(@order).

Does that make sense?

To summarize, you’ll want to create the nested resources  
with :name\_prefix, and you’ll want to know what kind of url you’ll be  
calling.

If you find a cleaner way to keep your routes untangled in your views  
and controllers, please let me know. I’m going to have this  
everywhere in an app I’m working on.

David

---

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [May 4, 2007, 12:22am UTC](https://www.ruby-forum.com/t/simple-problem-with-resources-routes/103297/4 "2007-05-04T00:22:17Z")

</div>

@Jason: your solution worked flawlessy 🙂 What I did missed, was that  
the  
“all\_” would go prefixed to the helpers… silly me

@David, thank you for your considerations! I’ve indeed resolved all my  
problems  
with the before\_filter in my offers\_controller, just before your mail  
arrived 🙂  
But I don’t understand why you’d need all those conditionals…  
Once I have the “all\_” prefix (that _now_ I understand), all my helpers  
worked  
as in:

all\_offers\_url() -\> /offers  
offers\_url(@category) -\> /categories/23/offers  
all\_offer\_url(@offer) -\> /offers/44  
formatted\_all\_offers\_path(:rss)  
all\_new\_offer\_url  
all\_preview\_new\_offer\_url()

They are a bit verbose, but they make definitely sense to me.

Am I missing something?

---

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [May 4, 2007, 12:34am UTC](https://www.ruby-forum.com/t/simple-problem-with-resources-routes/103297/5 "2007-05-04T00:34:18Z")

</div>

It’s elegant. I like it. This has been something that I’ve been  
wondering about for a while, but haven’t had time to implement. When  
I found the answer the other day, I was dying to try it out, but got  
called away. Thanks both of you.
