简体   繁体   中英

how to always use www as the subdomain when using link_to

I have a single secure payment page at secure.website.com. Using devise to sign out on that specific page, signing out breaks saying there's no user with 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).

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. I tried doing default_url_options but that didn't seem to work. How do I make sure all links are always www subdomain unless otherwise specified?

lastly, what i've been doing to guarantee a page is www unless on the secure page is this:

  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.

You can create a method 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.

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