views:

173

answers:

2

Is there a way to append something to the query string with no value set?

I would like to see this kind of URL being generated: http://local/things?magic.

What I'm looking for is when the user goes to http://local/other?magic then every URL in the generated page would contain magic in the end.

Following code is almost a solution but it gives me http://local/things?magic=. There is an extra = that I don't want there.

def default_url_options(options)
    if params.has_key?("magic")
        { :magic => "" }
    end
end

Setting { :magic => nil } will remove the magic entirely from the generated URLs.

A: 

Edited due to the changed question on the comment below:

Ah I see.

You could easily use the same URL for all devices and just separate by checking the request environment for HTTP_USER_AGENT.

But if this distinction is not enough (I think it is enough for more than 80% of all cases) you could do the following:

in config/routes.rb:

map.with_options :prefix => '/m', :format => 'mobile' do |mobile|
  mobile.resources :apples
  mobile.resource :user
  mobile.connect #...
end

# generates
# /m/apples
# /m/apples/new
# ...
# /m/user
# ... 

I have not tested this, but maybe you will have to add a MIME type mapping for :format => 'mobile'.

Overbryd
I would like to have different versions of pages and separate them through query string. The main version of a page would be http://mysite.com/somepage Then there could be a mobile version at http://mysite.com/somepage?mobileIdeally I would like to do this with sub-domains like http://m.mysite.com or http://mobile.mysite.com but not for starters.
Termopetteri
Have you seen my edit?
Overbryd
I have a few weeks break from Rails now.. but I forgot to say that my URLs have the locale setting also like this: http://mysite.com/fi/somepage and http://mysite.com/en/somepage I don't know how your solution will affect this..
Termopetteri
A: 

This is what cookies are for! cookies[:magic] = 'magic' and then it all automatically works just the way you're looking for. Not with URL query-string parameters, but whenever the browser requests another URL (until the browser is closed), it will include the cookie in the request.

Justice
The problem is that cookies break the meaning of URL (Uniform Resource Locator). If one is using cookies to show different versions of a page copy-pasting the URL to another user will behave as not intended, and also bookmarking the page will most likely not work as the user wanted. Cookies will break the "uniform" part of the URL meaning that there will be different versions of a page at the same URL which is not expected.But cookies are, at least, a quick and easy solution to the problem but it comes with some nasty side effects.
Termopetteri