# Better way to generate urls such as /:state/:city/:id without having to explicitly set :state and :c

**URL:** <https://www.ruby-forum.com/t/better-way-to-generate-urls-such-as-state-city-id-without-having-to-explicitly-set-state-and-c/145911>\
**Category:** Rails\
**Created:** [August 14, 2008, 11:11pm UTC](https://www.ruby-forum.com/t/better-way-to-generate-urls-such-as-state-city-id-without-having-to-explicitly-set-state-and-c/145911 "2008-08-14T23:11:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [August 14, 2008, 11:11pm UTC](https://www.ruby-forum.com/t/better-way-to-generate-urls-such-as-state-city-id-without-having-to-explicitly-set-state-and-c/145911/1 "2008-08-14T23:11:22Z")

</div>

Hi all -

Here’s my situation. We have a model Foo. All is well. Time for  
some SEO which gives us this as a route/url:

/foos/:id -\> /foos/123-acme-foo

Easy to do by defining Foo.to\_param. All is well. Time for some more  
SEO with this route/url:

/foos/:state/:city/:id -\> /foos/wa/olympia/123-acme-foo

And now I lose the convenience of being able to do:

\<%= link\_to ‘click’, foo\_path(@foo) %\>

As it no longer recognizes the route. So I’ve resorted to defining a  
Foo.url\_params which returns a hash containing the appropriate values  
and then do this:

\<%= link\_to ‘click’, foo\_path(@foo.url\_params) %\>

It works, but I’m annoyed.

I guess what I’m kind of looking for is a to\_params() method which if  
it exists will be used _instead_ of to\_param().

Or is there another way that will let me get back to doing  
foo\_path(@foo) and have the route match?

Thanks all!

-philip
