# Help with routing - prefix added automatically? - lookup action wanted

**URL:** <https://www.ruby-forum.com/t/help-with-routing-prefix-added-automatically-lookup-action-wanted/129655>\
**Category:** Rails\
**Created:** [February 20, 2008, 12:40pm UTC](https://www.ruby-forum.com/t/help-with-routing-prefix-added-automatically-lookup-action-wanted/129655 "2008-02-20T12:40:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tonypm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@tonypm](https://www.ruby-forum.com/u/tonypm)\
**Post date:** [February 20, 2008, 12:40pm UTC](https://www.ruby-forum.com/t/help-with-routing-prefix-added-automatically-lookup-action-wanted/129655/1 "2008-02-20T12:40:41Z")

</div>

I want to add a lookup action to my controller.

I am trying to stay in the REST framework, so I have used

map.resources :products

I tried adding :member=\>{:lookup=\>:get}

but this created a route that expected an id.

so I did this:

map.connect ‘product/  
lookup’, :controller=\>‘products’, :action=\>‘lookup’  
map.resources :products

This works but am I doing the right thing, because now I cannot use  
lookup\_products\_path  
SO, instead I used ‘products/lookup’ as a url string.

This is a link which is in a main menu in the layout, and works if I  
am not already serving an action from the products controller.

BUT, if I click on the link whilst I am already in a products view,  
the url returned is:  
products/products/lookup - and is obviously not found

NOW, I discovered that if I put the leading / in the url ‘/products/  
lookup’ then all is fine.

QUESTION: what is rails doing to the incoming request that causes the  
prefix to be automatically added. If it is intended, then it is a  
feature that I am sure could work to my advantage when trying to use  
namespaces (I have failed in this endeavour so far).

Please could someone help me, I really find the routing stuff hard to  
grasp. Is there any helpful documentation, I have the Agile rails  
book, the RESTful rails pdf and the peepcode rails 2 pdf, but I still  
cannot completely grasp it (I know I am being slow), but I do keep  
trying - lol.

Thanks  
Tony

---

<div class="post-metadata">

**Author:** ![tonypm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@tonypm](https://www.ruby-forum.com/u/tonypm)\
**Post date:** [February 20, 2008, 1:47pm UTC](https://www.ruby-forum.com/t/help-with-routing-prefix-added-automatically-lookup-action-wanted/129655/2 "2008-02-20T13:47:31Z")

</div>

If you want a custom REST action that doesn’t require an ID,  
use :collection instead of :member:

map.resources :products, :collection =\> { :lookup =\> :get }

---

<div class="post-metadata">

**Author:** ![tonypm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@tonypm](https://www.ruby-forum.com/u/tonypm)\
**Post date:** [February 21, 2008, 9:44pm UTC](https://www.ruby-forum.com/t/help-with-routing-prefix-added-automatically-lookup-action-wanted/129655/3 "2008-02-21T21:44:15Z")

</div>

On Feb 20, 12:46 pm, Thorsten [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If you want a custom REST action that doesn’t require an ID,  
> use :collection instead of :member:
> 
> map.resources :products, :collection =\> { :lookup =\> :get }

Thanks Thorsten - that worked. I had missed :collection.

I am still curious why rails automatically adds the prefix products to  
the url if the referrer was the products controller and I used the  
string url ‘products/lookup’?

Tonypm

---

<div class="post-metadata">

**Author:** ![tonypm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@tonypm](https://www.ruby-forum.com/u/tonypm)\
**Post date:** [February 25, 2008, 11:13am UTC](https://www.ruby-forum.com/t/help-with-routing-prefix-added-automatically-lookup-action-wanted/129655/4 "2008-02-25T11:13:49Z")

</div>

i guess that’s not rails, that’s your browser.

if you omit the leading “/”, it’s a relative path, so if the browser  
displays a page under [www.expample.com/products](http://www.expample.com/products), and on this page is a  
link to “products/lookup”, it means for the browser: “/products/  
products/lookup”, as this path is relative to the current  
“directory” (= URL).

Adding a leading slash makes it absolute to the base domain.

Rails link helpers take care of this automagically behind the scenes,  
but when your create links with hardcoded strings, you have to watch  
out for that.
