Rails are redirected to the same page, but add a subdomain

If the user is in

blah.com/items/index 

how to redirect them to

 de.blah.com/items/index 

in a way that will work for any page? I'm using right now

 <%= link_to 'German', root_url(:host => 'de' + '.' + request.domain + request.port_string) %> 

but redirects them to de.blah.com. How to save the rest of the url? I use rails 3.

+6
source share
3 answers

Keep it simple:

 redirect_to subdomain: 'de' 
+10
source
 <%= link_to 'German', params.merge({:host => with_subdomain(:de)}) %> 

in app / helpers / url_helper.rb

 module UrlHelper def with_subdomain(subdomain) subdomain = (subdomain || "") subdomain = "" if I18n.default_locale.to_s == subdomain subdomain += "." unless subdomain.empty? [subdomain, request.domain(tld_length), request.port_string].join end def url_for(options = nil) if options.kind_of?(Hash) && options.has_key?(:subdomain) options[:host] = with_subdomain(options.delete(:subdomain)) end super end def tld_length tld_length = case Rails.env when 'production' then 2 when 'development' then 0 else 0 end end end 
+3
source

Using the subdomain-fu plugin can be useful, it is also available as a gem.

+1
source

Source: https://habr.com/ru/post/886215/


All Articles