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

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

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

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

On Feb 20, 12:46 pm, Thorsten [email protected] 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

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, 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.