Routing, use parts of entered url as id / perma

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
domain.com/customer/screenshots
domain.com/customer/order
domain.com/partner/features
domain.com/partner/screenshots
domain.com/partner/order
domain.com/contact
domain.com/disclaimer
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 :wink:

Thanks in advance for any help,

  • ZoolWay

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
domain.com/customer/screenshots
domain.com/customer/order
domain.com/partner/features
domain.com/partner/screenshots
domain.com/partner/order
domain.com/contact
domain.com/disclaimer
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 :wink:

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

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
Rails Routing from the Outside In — Ruby on Rails Guides

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