1

When a user arrives to my site on an iPhone I want to present them with a page with 2 options:

1) Go get our app or 2) Continue on to the requested URL

I do this by checking the user agent in my application controller and rendering a view.

For option 2 to work properly on that view I want the link to preserve the original URL and append a query string parameter fullsite=1. I look for that query string parameter param in a before_filter in my application controller and set a cookie so that the user is not prompted again.

What is the cleanest way to append a query string parameter to a request.request_uri knowing that that request url may or may not have had a query string to start with? I know it is straight forward to check if the request had parameters but I'm wondering if there is a helper to append a parameter cleanly in either case (no params or with existing params) without evaluating things on my own. Seeking the correct "railsy" way to do this.

1 Answer 1

2

Probably the best way to do this is to add your flag to the params hash, which will already have all the other information you need to build that link (presuming you catch iPhones and render in a before_filter), and then build your link from the modified params hash:

In your before_filter:

params[:fullsite] = 1

And then in your view:

<%= link_to('Continue to the Full Site', params) %>

This way Rails gets to do all the messy work of escaping and appending your query strings - both :fullsite and whatever the original query string contained.

If you're just using :fullsite as a way to avoid redirecting iPhones through your iPhone portal multiple times, though, it seems like either of these approaches would work better:

  1. Set :fulltext in the session instead, or
  2. Only redirect users if they're on iPhones AND request.referer is something other than your site.

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Good suggestion to just add to the params hash in the before_filter. I want the selection to perist longer than simply the session but your suggested approaches make sense too. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.