Trying to use it as
= link_to "Me", user_url(@current_user.id, :subdomain => false)
In rails 3.1rc4
Apparently it has never worked.
What kind of behaviour would you expect from :subdomain => false?
:subdomain => false
Say you are on customer.mysite.com
Using blogs_url would produces something like customer.mysite.com/blogs. While I want just mysite.com/blogs
In rails 3, I had a url_for helper which got from this railscast - http://asciicasts.com/episodes/221-subdomains-in-rails-3
As shown in the cast, it used to work like this
<p><%= link_to "All Blogs", root_url(:subdomain => false) %></p>
However with 3.1 it doesn't work.
It doesn't work in v3.1 because the railscast suggests to override rails's internal behaviour (url_for method) and unfortunately the behaviour has now changed in v3.1.
Anyway if you still need to follow this fragile path just change /app/helpers/url_helper.rb so it now looks like:
subdomain = (subdomain || "")
subdomain += "." unless subdomain.empty?
def url_for(options = nil)
if options.kind_of?(Hash) && options.has_key?(:subdomain)
options[:host] = with_subdomain(options.delete(:subdomain))
options[:port] = request.port_string.gsub(':','') unless request.port_string.empty?
You may also need to adjust routes.rb due to new v3.1 requirements:
match '/' => 'blogs#show'
root :to => "blogs#index"
Tested with gem 'rails', '3.1.0.rc4' on a fresh project.
gem 'rails', '3.1.0.rc4'
Railscast aside, I think it makes sense to expect that users_url(:subdomain => false) should return "http://mydomain.com/users"
even if called @ http://subdomain.mydomain.com/
users_url(:subdomain => false)
Your "subdomain" can mean many things. What if you have sub.domain.mydomain.com ?
Will :subdomain => false return domain.mydomain.com or mydomain.com ?
This is very specific behavior which should be implemented in a plugin.
Have :subdomain => false to remove all subdomains is reasonable request.
We should probably only keep tld_length + 1 parts of the host if :subdomain => false.
tld_length + 1
Remove subdomain with :subdomain => '' on url_for [#2025 state:commit…
This was almost done with 2fe43b6
The missing thing was removing the '.' when an empty subdomain is specified which is what dcadenas@d6fd59c does.
I think it's better to follow the true / false convention like is already specified for :protocol,
that is we can already use :protocol => false like shown below:
true / false
:protocol => false
rails/actionpack/test/controller/url_for_test.rb : 125
W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => '//')
W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => false)
And so it's reasonable to add the same for :subdomain.
Here is my commit to make :subdomain => false possible:
I'm using Rails 3.1.0 and would find the :subdomain => false behaviour described by @daeltar very useful.
In my application, at the moment :subdomain => false does not change the URL. Additionally :subdomain => '' leaves a leading "." in the URL.
:subdomain => ''
Is there a work around in the pipeline? Would you recomment using @ksob's commit for the time being?
+1 for @ksob commit.
For now it looks like you can get rid of subdomains with:
root_url(:host => request.domain)
@szimek - Thanks! Your hint root_url(:host => request.domain) works for me.
:subdomain can now be specified with a value of false in url_for, all…
…owing for subdomain(s) removal from the host during link generation. Closes #2025
I notice this fix has not made it into 3-1-stable - presumably that means it will be in 3.2?
I'm having the same issue with 3.1 stable. I am unable to use the root_url(:host => request.domain) suggestion made above, as I'm attempting to redirect within my routes definition, e.g.:
match '/' => redirect('/signup/index')
match '*a' => redirect(:subdomain => false) # doesn't remove subdomain, resulting in redirect loop
# normal routes