简体   繁体   English

使用的link_to时,如何始终使用WWW的子域

[英]how to always use www as the subdomain when using link_to

I have a single secure payment page at secure.website.com. 我在secure.website.com上有一个安全的付款页面。 Using devise to sign out on that specific page, signing out breaks saying there's no user with id=sign_out. 使用devise登出该特定页面时,登出中断表示没有id = sign_out的用户。 I am guessing this is because when i'm on the secure page, all the links on that page also have secure.website.com/users/sign_out (for example). 我猜这是因为当我进入安全页面时,该页面上的所有链接也都具有secure.website.com/users/sign_out(例如)。

rather than always specifying that I want to use www as the url in my link_to...i want to set it to default to that. 而不是总是在link_to中指定我要使用www作为网址...我想将其设置为默认值。 I tried doing default_url_options but that didn't seem to work. 我尝试做default_url_options,但似乎没有用。 How do I make sure all links are always www subdomain unless otherwise specified? 除非另有说明,如何确保所有链接始终都是www子域?

lastly, what i've been doing to guarantee a page is www unless on the secure page is this: 最后,除非在安全页面上,否则我一直在保证网页是www:

  def always_use_www
    if params[:controller] != 'subscriptions' && request.subdomain != 'www'
      redirect_to :subdomain => "www"
    end
  end

i put this in the application controller as a before filter. 我把它放在应用程序控制器中作为前置过滤器。 this seems like a really poor way of doing it. 这似乎是一种非常糟糕的方法。 is there a 'right' way? 有没有“正确”的方法? I only want that one secure page to have a different subdomain...otherwise always use www. 我只希望一个安全页面具有不同的子域...否则,请始终使用www。

You can create a method default_url_options : 您可以创建一个方法default_url_options

def default_url_options
  {
    :host => "www.example.com",
    :only_path => false
  }
end

You can put this method in application controller of the subcontrollers. 您可以将此方法放在子控制器的应用程序控制器中。 To the best of my knowledge, you can only set the whole host, not subdomain. 据我所知,您只能设置整个主机,而不能设置子域。 In general, you can put any option url_for supports in this method. 通常,您可以在此方法中放置任何url_for支持的选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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