# Parameters in routing paths

**URL:** <https://www.ruby-forum.com/t/parameters-in-routing-paths/123297>\
**Category:** Rails\
**Created:** [December 16, 2007, 2:40am UTC](https://www.ruby-forum.com/t/parameters-in-routing-paths/123297 "2007-12-16T02:40:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cpher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2bfe46/32.png) [@cpher](https://www.ruby-forum.com/u/cpher)\
**Post date:** [December 16, 2007, 2:40am UTC](https://www.ruby-forum.com/t/parameters-in-routing-paths/123297/1 "2007-12-16T02:40:15Z")

</div>

Hello all.

I have what I think is a pretty common dilemma with Rails routing, and I  
haven’t been able to find or come up with a clean/DRY solution.

I have a home controller that is used for displaying real estate  
information. I want to be able to show this information on different  
platforms in different ways: desktop and mobile. I have seen similar  
topics for displaying custom iphone interfaces, but that usually  
involved using subdomains, which is not an option for me.

First, here is the relevant routing:

```
map.resource :home
map.with_options :controller => 'home' do |r|
    r.property '/property/:listing_id', :action =>
'show'
    r.property_details '/property/:listing_id/details', :action =>
'details'
    r.search '/search', :action =>
'search', :conditions => { :method => :post }
end
```

So what I want to do is have urls that looks like this:  
/desktop/property/1 -or-  
/mobile/property/2/details

I know that I can add a route like this:

```
r.property '/:platform/property/:listing_id', :action=>'show',
:target_platform => /(desktop|mobile)/i
```

The problem with this is that it isn’t DRY, as every named route would  
now need the /:platform/ prefix added to it. Additionally, every URL I  
want to call would explicitly need to specify the platform, like so  
(again not DRY):  
property\_path(:listing =\> 1, :platform =\> ‘desktop’)

Finally, I know you can add custom respond\_to with Rails 2.0 (e.g  
request.format = :iphone), but I want to allow the user to select the  
format, not purely based on detection.

Anyway, if you are still reading this, thank you. I’d greatly  
appreciate any sort of help. I have seen similar discussions  
surrounding internationalization (e.g. /en/home/ vs /fr/home).

Thanks again.

---

<div class="post-metadata">

**Author:** ![cpher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2bfe46/32.png) [@cpher](https://www.ruby-forum.com/u/cpher)\
**Post date:** [December 16, 2007, 3:14am UTC](https://www.ruby-forum.com/t/parameters-in-routing-paths/123297/2 "2007-12-16T03:14:44Z")

</div>

On Sun, 2007-12-16 at 02:40 +0100, Chris J. wrote:

> ‘search’, :conditions =\> { :method =\> :post }  
> :target\_platform =\> /(desktop|mobile)/i  
> format, not purely based on detection.
> 
> Anyway, if you are still reading this, thank you. I’d greatly  
> appreciate any sort of help. I have seen similar discussions  
> surrounding internationalization (e.g. /en/home/ vs /fr/home).
> 
> Thanks again.

* * *

rails 2.x has a very elegant method of handling formats and therefore  
you wouldn’t have to go through the routing issue at all

Craig

---

<div class="post-metadata">

**Author:** ![cpher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2bfe46/32.png) [@cpher](https://www.ruby-forum.com/u/cpher)\
**Post date:** [December 16, 2007, 8:33am UTC](https://www.ruby-forum.com/t/parameters-in-routing-paths/123297/3 "2007-12-16T08:33:32Z")

</div>

Craig W. wrote:

> On Sun, 2007-12-16 at 02:40 +0100, Chris J. wrote:
> 
> > ‘search’, :conditions =\> { :method =\> :post }  
> > :target\_platform =\> /(desktop|mobile)/i  
> > format, not purely based on detection.
> > 
> > Anyway, if you are still reading this, thank you. I’d greatly  
> > appreciate any sort of help. I have seen similar discussions  
> > surrounding internationalization (e.g. /en/home/ vs /fr/home).
> > 
> > Thanks again.
> 
> * * *
> 
> rails 2.x has a very elegant method of handling formats and therefore  
> you wouldn’t have to go through the routing issue at all
> 
> Craig

Thanks for the reply Craig. The issue I see, however, has to do with  
allowing the user to switch formats. It is one thing to be able to  
simply detect that the current user has an mobile device and render  
accordingly, but it is something else it seems to allow a user to switch  
format on demand (say, if they have a more capable device) and keep that  
same platform variable set until it is otherwise changed (hence my  
desire to propagate this platform variable in the URL:  
/:platform/:some\_route).

Anyway, even with Rails 2.0, I am not sure how to keep track of the  
desired output format (I don’t want to use session variables in this  
case).

I hope that somewhat clarifies my goal.  
Thanks again.
