# Routing, use parts of entered url as id / perma

**URL:** <https://www.ruby-forum.com/t/routing-use-parts-of-entered-url-as-id-perma/170505>\
**Category:** Rails\
**Created:** [June 18, 2009, 9:41am UTC](https://www.ruby-forum.com/t/routing-use-parts-of-entered-url-as-id-perma/170505 "2009-06-18T09:41:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZoolWay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5f8ce5/32.png) [@ZoolWay](https://www.ruby-forum.com/u/ZoolWay)\
**Post date:** [June 18, 2009, 9:41am UTC](https://www.ruby-forum.com/t/routing-use-parts-of-entered-url-as-id-perma/170505/1 "2009-06-18T09:41:33Z")

</div>

Hello,

I just started ruby on rails a week ago and know cannot find any  
information which helps me for my desired routing. At the moment I  
cannot image that this should be too hard.

I’d like to use urls like the following to display a simple page:  
[domain.com/customer/features](http://domain.com/customer/features)  
[domain.com/customer/screenshots](http://domain.com/customer/screenshots)  
[domain.com/customer/order](http://domain.com/customer/order)  
[domain.com/partner/features](http://domain.com/partner/features)  
[domain.com/partner/screenshots](http://domain.com/partner/screenshots)  
[domain.com/partner/order](http://domain.com/partner/order)  
[domain.com/contact](http://domain.com/contact)  
[domain.com/disclaimer](http://domain.com/disclaimer)  
[domain.com/home](http://domain.com/home)

This is deadly simple possible using sinatra with the following  
routes:  
get ‘/’ do  
MyClass::MyMethod()  
end

get ‘/:pageTitle’ do  
MyClass::ShowPage(params[:pageTitle], nil)  
end

get ‘/:audienceTitle/:pageTitle’ do  
MyClass::ShowPage(params[:pageTitle], params[:audienceTitle])  
end

In MyClass::ShowPage() I can do the necessary lookup for the page  
depending on provided parameters.  
Unfortunately I don’t know how to pass part of the url as param to my  
routing. I tried this and similars in routes.rb:  
map.foo ‘foo/:pageTitle’, :controller =\> ‘pages’, :action =\>  
‘show’, :id =\> Page.find\_by\_pageTitle(:pageTitle)[0].id

But this does not work. And please, I do not want to start discussion  
about permalink pro/con 😉

Thanks in advance for any help,

- ZoolWay

---

<div class="post-metadata">

**Author:** ![ZoolWay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5f8ce5/32.png) [@ZoolWay](https://www.ruby-forum.com/u/ZoolWay)\
**Post date:** [June 18, 2009, 12:14pm UTC](https://www.ruby-forum.com/t/routing-use-parts-of-entered-url-as-id-perma/170505/2 "2009-06-18T12:14:11Z")

</div>

ZoolWay wrote:

> Hello,
> 
> I just started ruby on rails a week ago and know cannot find any  
> information which helps me for my desired routing. At the moment I  
> cannot image that this should be too hard.
> 
> I’d like to use urls like the following to display a simple page:  
> [domain.com/customer/features](http://domain.com/customer/features)  
> [domain.com/customer/screenshots](http://domain.com/customer/screenshots)  
> [domain.com/customer/order](http://domain.com/customer/order)  
> [domain.com/partner/features](http://domain.com/partner/features)  
> [domain.com/partner/screenshots](http://domain.com/partner/screenshots)  
> [domain.com/partner/order](http://domain.com/partner/order)  
> [domain.com/contact](http://domain.com/contact)  
> [domain.com/disclaimer](http://domain.com/disclaimer)  
> [domain.com/home](http://domain.com/home)
> 
> This is deadly simple possible using sinatra with the following  
> routes:  
> get ‘/’ do  
> MyClass::MyMethod()  
> end
> 
> get ‘/:pageTitle’ do  
> MyClass::ShowPage(params[:pageTitle], nil)  
> end
> 
> get ‘/:audienceTitle/:pageTitle’ do  
> MyClass::ShowPage(params[:pageTitle], params[:audienceTitle])  
> end
> 
> In MyClass::ShowPage() I can do the necessary lookup for the page  
> depending on provided parameters.  
> Unfortunately I don’t know how to pass part of the url as param to my  
> routing. I tried this and similars in routes.rb:  
> map.foo ‘foo/:pageTitle’, :controller =\> ‘pages’, :action =\>  
> ‘show’, :id =\> Page.find\_by\_pageTitle(:pageTitle)[0].id
> 
> But this does not work. And please, I do not want to start discussion  
> about permalink pro/con 😉
> 
> Thanks in advance for any help,
> 
> - ZoolWay

Hey ZoolWay

I you shouldn’t be calling find in your routes file at all.

try map.connect “foo/:pageTitle”, :controller =\> “pages”, :action =\>  
“show”

and then in your show action you can use:  
Page.find\_by\_pageTitle(:pageTitle)

Also - I would try to stick to the convention of underscoring column  
names rather than camelCase.

Gavin

> **[Ruby on Rails tips & Tutorials](https://handyrailstips.com)**
>
> Ruby on Rails tips and tutorials

---

<div class="post-metadata">

**Author:** ![ZoolWay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5f8ce5/32.png) [@ZoolWay](https://www.ruby-forum.com/u/ZoolWay)\
**Post date:** [June 18, 2009, 4:11pm UTC](https://www.ruby-forum.com/t/routing-use-parts-of-entered-url-as-id-perma/170505/3 "2009-06-18T16:11:10Z")

</div>

I think you should be looking at your models here - both Customer and  
Partner look like candidates for a has\_and\_belongs\_to\_many association  
with Feature, Screenshot, and Order. Once you get this sorted the  
routes you are interested should come naturally out of your various  
controller methods.

Take a look at:

[Active Record Associations — Ruby on Rails Guides](http://guides.rubyonrails.org/association_basics.html)  
[Rails Routing from the Outside In — Ruby on Rails Guides](http://guides.rubyonrails.org/routing.html)

---

<div class="post-metadata">

**Author:** ![ZoolWay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/5f8ce5/32.png) [@ZoolWay](https://www.ruby-forum.com/u/ZoolWay)\
**Post date:** [June 18, 2009, 5:59pm UTC](https://www.ruby-forum.com/t/routing-use-parts-of-entered-url-as-id-perma/170505/4 "2009-06-18T17:59:59Z")

</div>

Thank you both for you information.

I’ll look into Rick’s links soon. For the first try Gavin’s suggestion  
work out quite well. Even with a second route with two params it works  
well!

Best regards,

- ZoolWay
