Question

Sorry in advance for the question length, but I wanted to summarize my knowledge up to this point and try to hone in on the right solution quicker.

I have an application that will sometimes be embedded on another web page. To simplify the explanation, imagine a single server, that serves up either a normal app or a version of the app for embedding, depending on the domain name.

For the latter domain, links that the app generates with link_to, product_url, image_tag, etc., should be absolute (http://servername/images/image_01.jpg), and for the former, they should be relative (I think, unless I can't solve this problem elegantly).

I know I can generate absolute URLs using, for example, product_url(:id, [...], :host => request.host, :port => request.port), but I see two problems with this approach. Firstly, it seems inefficient to rewrite this code in every link request in every view. Secondly, when the application is running on its own, not embedded, I would rather have relative URLs for efficiency and best practices.

For static assets, it's possible to do the following, in config/environments/whatever.rb: config.action_controller.asset_host = "http://assets.example.com"

But, this does not affect links generated by Rails.

I've been searching for an answer for this for a couple of days, and have nothing current for Rails 3. There is an old bit of code here that does the trick for Rails 2:

http://opensoul.org/2009/2/21/force-absolute-urls

But it doesn't seem to work for Rails 3. So...

  1. Should I make some helper function that detects if the links are going to be embedded?
  2. Patch url_for to make it work how I want, without application view changes?
  3. Rewrite all my view files anyway?

Thanks in advance to any replies.

Cheers,

Zev

Was it helpful?

Solution

I've hunted through the sources and it seems Rails 3.0 don't support setting that as a default. I'd probably go for monkey-patching url_for unless you have a really good reason not to.

There is an option :only_path that if you either set to default true or default something based on the domain. Currently it defaults to the presence of the :host option. This patch could possibly break some not so well coded plugins, make sure you test them well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top