Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

named_url and url_for: subdomain => false doesn't work #2025

Closed
abhishiv opened this Issue · 15 comments
@abhishiv

Trying to use it as

= link_to "Me", user_url(@current_user.id, :subdomain => false)

In rails 3.1rc4

Apparently it has never worked.

@exviva

What kind of behaviour would you expect from :subdomain => false?

@abhishiv

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.

@ksob

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:

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

  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?
    end  
    super  
  end
end

You may also need to adjust routes.rb due to new v3.1 requirements:

MyBlog310::Application.routes.draw do
  resources :blogs

  constraints(Subdomain) do
    match '/' => 'blogs#show'
  end
  root :to => "blogs#index"
end

Tested with gem 'rails', '3.1.0.rc4' on a fresh project.

@Bodacious

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/

@dmathieu
Collaborator

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.

@daeltar

Have :subdomain => false to remove all subdomains is reasonable request.

@exviva

We should probably only keep tld_length + 1 parts of the host if :subdomain => false.

@Bodacious

@exviva Agreed!

@dcadenas

This was almost done with 2fe43b6
The missing thing was removing the '.' when an empty subdomain is specified which is what dcadenas@d6fd59c does.

@ksob

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:

rails/actionpack/test/controller/url_for_test.rb : 125

def test_without_protocol
  add_host!
  assert_equal('//www.basecamphq.com/c/a/i',
    W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => '//')
  )
  assert_equal('//www.basecamphq.com/c/a/i',
    W.new.url_for(:controller => 'c', :action => 'a', :id => 'i', :protocol => false)
  )
end

And so it's reasonable to add the same for :subdomain.
Here is my commit to make :subdomain => false possible:
ksob@cb4263e

@smaboshe

Hello.

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.

Is there a work around in the pipeline? Would you recomment using @ksob's commit for the time being?

Thanks!

@szimek

+1 for @ksob commit.

For now it looks like you can get rid of subdomains with:

root_url(:host => request.domain)
@smaboshe

@szimek - Thanks! Your hint root_url(:host => request.domain) works for me.

@ksob ksob referenced this issue from a commit in ksob/rails
@ksob ksob :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
de942e5
@ksob ksob referenced this issue from a commit in ksob/rails
@ksob ksob :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
bd0695c
@josevalim josevalim closed this in b039069
@ksob ksob referenced this issue from a commit in ksob/rails
@ksob ksob :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
28de950
@tgjones

I notice this fix has not made it into 3-1-stable - presumably that means it will be in 3.2?

@mhuggins

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.:

Dating::Application.routes.draw do
  constraints(SubdomainConstraint) do
    resource :signup
    match '/' => redirect('/signup/index')
    match '*a' => redirect(:subdomain => false)  # doesn't remove subdomain, resulting in redirect loop
  end

  # normal routes
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.