# Question about params for resource routes

**URL:** <https://www.ruby-forum.com/t/question-about-params-for-resource-routes/72250>\
**Category:** Rails\
**Created:** [October 16, 2006, 12:55am UTC](https://www.ruby-forum.com/t/question-about-params-for-resource-routes/72250 "2006-10-16T00:55:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [October 16, 2006, 12:55am UTC](https://www.ruby-forum.com/t/question-about-params-for-resource-routes/72250/1 "2006-10-16T00:55:33Z")

</div>

I have a application that I am building with edge rails. I am using the  
map.resources in my routes.config and this is what I have

map.resources :categories do |categories|  
categories.resources :businesses do |businesses|  
businesses.resources :ratings  
end  
categories.resources :criterias  
end

This is the first application that I attempted this with, so if anything  
is  
wrong please point it out to me. My question is that when using  
ratings\_url  
I have to supply @category, @business to get the url to be created.  
While  
this is fine, when I want to use business\_url I have to use @category in  
the  
views like so,

\<%= link\_to ‘Show’, business\_path(@category, @business) %\>

but in my business controller it is like this,

redirect\_to business\_url(@business)

and I do not have to supply the @category. Is it because one uses a  
path  
while the other uses a url? I have tests that run on all of this and  
they  
all pass, so I know that the code is correct. Why does it work this  
way?  
Why do I not have to supply the @category for business\_url but I do for  
rating\_url?

–  
John H.

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [October 16, 2006, 1:59am UTC](https://www.ruby-forum.com/t/question-about-params-for-resource-routes/72250/2 "2006-10-16T01:59:38Z")

</div>

Hi –

On Sun, 15 Oct 2006, John H. wrote:

> This is the first application that I attempted this with, so if anything is
> 
> and I do not have to supply the @category. Is it because one uses a path  
> while the other uses a url? I have tests that run on all of this and they  
> all pass, so I know that the code is correct. Why does it work this way?  
> Why do I not have to supply the @category for business\_url but I do for  
> rating\_url?

I have a couple of follow-up questions:

What does business\_url(@business) look like? Also, what are the id’s  
of @category and @business in your tests? (I ask the second because I  
remember having a case, which I can’t quite reconstruct, where the  
fact that two objects had the same id actually caused a URL to work by  
coincidence.)

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [October 16, 2006, 2:09am UTC](https://www.ruby-forum.com/t/question-about-params-for-resource-routes/72250/3 "2006-10-16T02:09:39Z")

</div>

Hey David,

The url for business\_url look like

categories/:category\_id/businesses/:business\_id

the url for rating\_url looks like

categories/:category\_id/businesses/:business\_id/ratings/:rating\_id

The id’s in the business test are

:id for @business

:category\_id for @category

the id’s in the rating test are

:id for @rating

:category\_id for @category

:business\_id for @business

Everything works great and no errors are being thrown, I am just curious  
why  
business\_url(@business) only requires the @category in the views and not  
the  
controller while the rating\_url requires @category and @business params  
at  
all times, in both the views and controller.

On 10/15/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > categories.resources :businesses do |businesses|  
> > created. While  
> > and I do not have to supply the @category. Is it because one uses a  
> > What does business\_url(@business) look like? Also, what are the id’s  
> > Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
> > DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
> > [1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
> > [2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)
> 
> >

–  
John H.

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [October 16, 2006, 2:37am UTC](https://www.ruby-forum.com/t/question-about-params-for-resource-routes/72250/4 "2006-10-16T02:37:42Z")

</div>

Sorry, about that.

You actually led me to the answer by asking the right questions. What  
was  
happening was that business\_url(@business) was actually producing  
/categories/:business\_id/businesses, so the @business.id was being used  
for  
the category id in the url. This was not being caught by tests or  
controller because they where not being tested for(I thought I was  
testing  
for it). The test now accounts for this and it is all working great.  
Sometimes bugs simply need to be talked about I guess to find the simple  
answer 🙂

Thanks David,

John

On 10/15/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > categories/:category\_id/businesses/:business\_id  
> > the id’s in the rating test are  
> > I may be asking it the wrong way… I’m trying to see where the  
> > [1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
> > [2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)
> 
> >

–  
John H.

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [October 16, 2006, 2:15am UTC](https://www.ruby-forum.com/t/question-about-params-for-resource-routes/72250/5 "2006-10-16T02:15:40Z")

</div>

Hi –

On Sun, 15 Oct 2006, John H. wrote:

> :business\_id for @business  
> Sorry, I didn’t put it clearly. I meant: what is the actual return  
> value of a call to business\_url(@business), for some particular  
> @business, and what are the id numbers of the objects in your tests?  
> I may be asking it the wrong way… I’m trying to see where the  
> :category\_id value came from if you didn’t pass @category to the  
> method.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [October 16, 2006, 2:51am UTC](https://www.ruby-forum.com/t/question-about-params-for-resource-routes/72250/6 "2006-10-16T02:51:44Z")

</div>

David,

it will be great when you get it done. The only thing that I see a  
problem  
with in the way it is done now is that you get problems like I just had  
where I was forgetting a call in my test and it still passed with  
business\_url(@business), whereas from what I understood the singular  
call  
would produce the “show” page and instead it was redirecting to the  
“index”  
because the two params where not present.

On 10/15/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > happening was that business\_url(@business) was actually producing
> 
> and also:

–  
John H.

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [October 16, 2006, 2:47am UTC](https://www.ruby-forum.com/t/question-about-params-for-resource-routes/72250/7 "2006-10-16T02:47:44Z")

</div>

Hi –

On Sun, 15 Oct 2006, John H. wrote:

> Sorry, about that.
> 
> You actually led me to the answer by asking the right questions. What was  
> happening was that business\_url(@business) was actually producing  
> /categories/:business\_id/businesses, so the @business.id was being used for  
> the category id in the url. This was not being caught by tests or  
> controller because they where not being tested for(I thought I was testing  
> for it). The test now accounts for this and it is all working great.  
> Sometimes bugs simply need to be talked about I guess to find the simple  
> answer 🙂

Cool. You did indeed find just what I was suspecting.

I’ve found it cumbersome to have to always supply the nesting  
arguments, and I’m working on a plugin (so far just a vicious hack of  
the Rails source 🙂 that will allow you to do:

business\_url(@business)

and have it infer:

business\_url(@business.category, @business)

and also:

businesses\_url(@business)

would expand to:

businesses\_url(@business.category)

rather than using @business’s id in the :category\_id slot. Still a  
work in progress…

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![jh100000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b2d939/32.png) [@jh100000](https://www.ruby-forum.com/u/jh100000)\
**Post date:** [October 16, 2006, 3:07am UTC](https://www.ruby-forum.com/t/question-about-params-for-resource-routes/72250/8 "2006-10-16T03:07:45Z")

</div>

Hi –

On Sun, 15 Oct 2006, John H. wrote:

> > was  
> > Cool. You did indeed find just what I was suspecting.
> 
> David,
> 
> it will be great when you get it done. The only thing that I see a problem  
> with in the way it is done now is that you get problems like I just had  
> where I was forgetting a call in my test and it still passed with  
> business\_url(@business), whereas from what I understood the singular call  
> would produce the “show” page and instead it was redirecting to the “index”  
> because the two params where not present.

My current vicious hack does some heuristics to determine whether the  
call is singular or plural – basically, checking for an :id segment  
– and proceeds on that basis. I’m still probing the source and  
trying things out, though. It will probably be dependent on  
conformity to pretty predictable conventions, on the part of the  
programmer, so the inferences can be made.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)
