Sean Mountcastle
2006-Jan-18 15:47 UTC
[Rails] How do you get link_to to work correctly when using subdomains as account keys?
When using subdomains as account keys (as per this HowTo: http://wiki.rubyonrails.org/rails/pages/HowToUseSubdomainsAsAccountKeys), how can you get link_to to spit out the correct URL (i.e. account123.domain.com)? <%= link_to "My Account", { :action => "show", :id => "account123" } %> would generate "http://www.domain.com/show/account123" I don''t have any special routes setup (in routes.rb), but I don''t think that would help for this purpose. Thanks, Sean
Eric Hodel
2006-Jan-18 18:20 UTC
[Rails] How do you get link_to to work correctly when using subdomains as account keys?
On Jan 18, 2006, at 7:45 AM, Sean Mountcastle wrote:> When using subdomains as account keys (as per this HowTo: > http://wiki.rubyonrails.org/rails/pages/ > HowToUseSubdomainsAsAccountKeys), > how can you get link_to to spit out the correct URL (i.e. > account123.domain.com)? > > <%= link_to "My Account", { :action => "show", :id => "account123" } > %> would generate "http://www.domain.com/show/account123" > > I don''t have any special routes setup (in routes.rb), but I don''t > think that would help for this purpose.in ApplicationControllor: def url_for(options = {}, *params) # :nodoc: if options[:subdomain] then options[:only_path] = false host = [] host << options.delete(:subdomain) host << @request.subdomains[1..-1] if @request.subdomains.size > 1 host << @request.domain options[:host] = host.join ''.'' end return super(options, *params) end -- Eric Hodel - drbrain@segment7.net - http://segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com
Sean Mountcastle
2006-Jan-18 19:38 UTC
[Rails] How do you get link_to to work correctly when using subdomains as account keys?
Eric,> in ApplicationControllor: > > def url_for(options = {}, *params) # :nodoc: > if options[:subdomain] then > options[:only_path] = false > host = [] > host << options.delete(:subdomain) > host << @request.subdomains[1..-1] if @request.subdomains.size > > 1 > host << @request.domain > options[:host] = host.join ''.'' > end > > return super(options, *params) > endThank you very much, though this breaks when using WEBrick (or Lighty on a port other than 80) so I added the following after options[:host] = host.join ''.'' options[:host] = [ options[:host], @request.port ].join '':'' unless @request.port == 80 It looks ugly, but it works. Regards, Sean