Rails,是否可以部分使用SSL

时间:2013-04-12 02:43:35

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

是否可以将http用于某些请求,并将https用于其他请求,例如

http://i.mysite.com/

https://mysite.com/

感谢

1 个答案:

答案 0 :(得分:1)

是:

before_filter :https_redirect

def https_redirect
  if request.ssl? && !use_https? || !request.ssl? && use_https?
    protocol = request.ssl? ? "http" : "https"
    flash.keep
    redirect_to protocol: "#{protocol}://", status: :moved_permanently
  end
end

def use_https?
  controller_name == "abc"
end

(我从某个地方接受此代码,我不记得所以我无法提供学分......但我在项目中使用它并且它有效。)

更新:我从RailsCasts中获取代码哈哈,谢谢Ryan Bates。