简体   繁体   中英

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.

事实证明我想要使用'path'help',而不是'url'帮助器:(。所以,为了完成这项工作,你需要使用root_url,而不是root_path等。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM