99miles
99miles

Reputation: 11212

Subdomains in Rails 3 in url helpers

I'm trying to get this working, but it keeps using the subdomain I'm already at http://railscasts.com/episodes/221-subdomains-in-rails-3

In url_for when I debug, it shows options[:host] as what I'd expect (subdomain.domain.com), but super just return 'accounts/sign_up' without the full path. That doesn't feel right.

What's going on? Here's what I have:

module UrlHelper

  def with_subdomain(subdomain)
    subdomain = (subdomain || "")
    subdomain += "." unless subdomain.empty?
    [subdomain, request.domain, 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 set_mailer_url_options
    ActionMailer::Base.default_url_options[:host] = with_subdomain(request.subdomain)
  end

end

I tried devise and non-devise helpers:

  = link_to 'Plan', new_plan_path(:subdomain => 'mysubdomain')
  = link_to "Sign up", new_registration_path(resource_name, :subdomain => 'mysubdomain');

UPDATE: When I follow the code, it eventually calls:

_routes.url_for((options || {}).reverse_merge!(url_options).symbolize_keys)

where

_routes.url_for(options || {}) # -> "/accounts/sign_in"

and

(url_options).symbolize_keys # -> {:host=>"test.lvh.me:3000", :protocol=>"http://", :_path_segments=>{:action=>"new", :controller=>"devise/passwords"}, :script_name=>""}

Still not sure how to work around it.

Upvotes: 1

Views: 2729

Answers (1)

99miles
99miles

Reputation: 11212

Turns out I want using the 'path' helpers', not the 'url' helpers :(. So, to make this work you need to use root_url, not root_path, etc.

Upvotes: 4

Related Questions