Parameters in routing paths

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.

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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs