Change protocol to https in all rails helpers

后端 未结 9 1613
情书的邮戳
情书的邮戳 2020-12-10 12:55

Rails 3.1+ I want my url helpers to use the https protocol without having to specify it in every helper I call. After searching around I\'ve found various ways but none work

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 13:31

    In Rails 3.2.1, by default force_ssl is true, let's check

    1. Open config/environments/production.rb and search "config.force_ssl"

    config.force_ssl = true - no need to change

    now in config/environments/development.rb - no need to place config.force_ssl, it should work, because your server running locally.

    Ok, here is the other view

    if !request.ssl?
      "https://" + request.host + request.request_uri
    elsif request.ssl?
      "http://" + request.host + request.request_uri
    end
    

    Add def in helper base on above if else and in the ActionView::Helpers, there is a url_for method that might get you what you want if you start using that.

提交回复
热议问题