3

I have a partners subdomain which serves content using a controller.

The urls are like: http://partners.example.com/...

Is there a way I can force all _path and _url functions to return links to the root www subdomain? Since the resources my links refer to are not on the subdomain itself and only on the root domain (and I don't want to serve duplicates).

Jacob
  • 6,317
  • 10
  • 40
  • 58

4 Answers4

7

In Rails 3.1, it can be done with:

resource_url(:subdomain => 'www')

But, the helper resource_path does not accept the subdmain key.

sailor
  • 7,834
  • 3
  • 26
  • 34
2

This is similar to my answer here: How to automatically set all links to nofollow in Rails

Instead of no-following (like in my other answer), you could:

def link_to(name, options = {}, html_options = {})
  html_options.merge!(:host => with_subdomain("www"))
  super(name, options, html_options)
end

def with_subdomain(subdomain)
  subdomain = (subdomain || "")
  subdomain += "." unless subdomain.empty?
  [subdomain, request.domain, request.port_string].join
end

with_subdomain method is from http://railscasts.com/episodes/221-subdomains-in-rails-3

Community
  • 1
  • 1
Jesse Wolgamott
  • 40,197
  • 4
  • 83
  • 109
2

I'd set the default_url_options to include your main host:

ActionController::Base.default_url_options = {:host => '...'}

The options you pass to link_to will override these but, as the name suggests, this allows you to provide default values.

Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174
0

There is an excellent rails cast on this subject here:

http://railscasts.com/episodes/221-subdomains-in-rails-3

Your question was too vague without actual code snippets of your controller requests.

This should get you your answer though. Just customize to your needs.