【问题标题】:How to Force SSL to Specific Named Route in Rails如何强制 SSL 到 Rails 中的特定命名路由
【发布时间】:2013-12-23 06:38:12
【问题描述】:

我有一个 Rails 应用程序,其中我使用 force_ssl 方法

def force_ssl # to use https for payment page
  if !Rails.env.development? && !request.ssl?
    redirect_to :protocol => 'https'
  end
end

对于带有命名路由的特定动作名称“abc”

match '/find-a-abc' => "home#abc"

当我转到 URL 时

http://local.demo.com/find-a-abc

它会重定向到https://local.demo.com/abc

任何解决方案?所以它会redirect_to特定的路由,而不是在使用https协议时重定向到一个Action。

【问题讨论】:

  • force_ssl 定义为before_filter 工作吗?
  • 是的,它工作得很好,但我的问题是它重定向到操作而不是命名路径。

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


【解决方案1】:

您似乎没有在此处提供命名路由(即match '/find-a-abc' => "home#abc", :as => :named_route)。您需要执行此操作并调用 named_route_url 而不仅仅是控制器和操作来获取正确的 URL。

如果您希望始终使用 SSL 处理特定路由,您可以像这样定义路由:

scope :protocol => 'https://', :constraints => { :protocol => 'https://' } do
  match '/find-a-abc' => "home#abc", :as => :abc
end

那么abc_url 应该始终是“https://local.demo.com/find-a-abc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多