【问题标题】:Subdomains in Rails 3 in url helpersRails 3 中 url 帮助程序中的子域
【发布时间】:2011-08-24 23:02:11
【问题描述】:

我正在尝试让它工作,但它一直在使用我已经在 http://railscasts.com/episodes/221-subdomains-in-rails-3 的子域

在我调试的 url_for 中,它显示 options[:host] 正如我所期望的那样 (subdomain.domain.com),但 super 只返回 'accounts/sign_up' 而没有完整路径。感觉不对。

发生了什么事?这是我所拥有的:

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

我尝试了设计和非设计助手:

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

更新: 当我按照代码进行操作时,它最终会调用:

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

在哪里

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

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

仍然不确定如何解决它。

【问题讨论】:

  • 如果你这样做会怎样super(options)
  • 导轨 3.0.7。 super(options) 也没有这样做。
  • subdomain = (subdomain || "") 可以缩短为 subdomain ||= "" 或者如果 subdomain 是字符串或 nil 您可以删除此行并进行更改,因为连接方法调用 #to_s 数组中的每个元素和 @987654331 @返回一个空字符串。
  • @jigfox -- 我刚刚从 railscast 中获取了该代码。知道如何进行这项工作吗?

标签: ruby-on-rails ruby-on-rails-3


【解决方案1】:

原来我想使用“路径”助手,而不是“url”助手 :(。因此,要完成这项工作,您需要使用 root_url,而不是 root_path 等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多