首页 \ 问答 \ Rails 3:在某些部分强制HTTP,在其他部分强制使用HTTPS?(Rails 3: Forcing HTTP in some parts, and Forcing HTTPS in others?)

Rails 3:在某些部分强制HTTP,在其他部分强制使用HTTPS?(Rails 3: Forcing HTTP in some parts, and Forcing HTTPS in others?)

我目前正试图找到一种方法来强制某些路由的http(更快,不需要安全性)和强制https用于更敏感的页面。 我看到了答案: Rails 3 SSL弃用 ,但它似乎不起作用。

我在根网址周围添加了一个范围:

scope :constraints => { :protocol => 'http' } do
  root :to => 'site#index'
end

但是当我试图转到localhost:3000时,它只是抛出了错误No route matches [GET] "/"

是否有一种干净的方法在某些地方强制http并在其他地方强制使用https(我可以在我的控制器中使用force_ssl ,但是有一个force_http吗?)路线的范围看起来很干净,但它对我不起作用。 我做错了吗?

这样的方法怎么样?

CONTROLLERS_THAT_REQUIRE_SSL = ['check_out']
def ensure_proper_protocol
  unless Rails.env.development? || Rails.env.test?
    if request.protocol["https"] && !CONTROLLERS_THAT_REQUIRE_SSL.include?(params[:controller])
      redirect_to "http://" + request.host + request.path
    end
  end
end

I'm currently trying to find out a way to force http for some routes (faster, no need for security) and forcing https for more sensitive pages. I saw the answer to this: Rails 3 SSL Deprecation , but it doesn't seem to work.

I added a scope around my root url:

scope :constraints => { :protocol => 'http' } do
  root :to => 'site#index'
end

But it just threw the error No route matches [GET] "/" when I tried to go to localhost:3000.

Is there a clean way to force http in some places and force https in others (I could use force_ssl in my controller, but is there a force_http?) The scope around the routes looks clean, but it isn't working for me. Am I doing it wrong?

How about a method like this?

CONTROLLERS_THAT_REQUIRE_SSL = ['check_out']
def ensure_proper_protocol
  unless Rails.env.development? || Rails.env.test?
    if request.protocol["https"] && !CONTROLLERS_THAT_REQUIRE_SSL.include?(params[:controller])
      redirect_to "http://" + request.host + request.path
    end
  end
end

原文:https://stackoverflow.com/questions/18518720
更新时间:2021-09-16 10:09

最满意答案

由于doSomething [1][2,1] ,因此您的代码相当于:

afterThreeTurns = do
   first <- ["test"]
   x <- [2,1]
   return first

这与列表理解[ first | first <- ["test"], x <- [2,1] ] [ first | first <- ["test"], x <- [2,1] ] ,这就解释了为什么你得到长度为2的列表。

请注意,变量x在任何地方都没有被引用,所以这也可以写成:

afterThreeTurns = do
   first <- ["test"]
   _ <- [2,1]
   return first

这是使用IO monad的一个类似情况。 代码:

thirdLine = do
  getLine
  getLine
  x <- getLine
  putStrLn $ "The third line is: " ++ x

是相同的:

thirdLine = do
  _ <- getLine
  _ <- getLine
  x <- getLine
  putStrLn $ "The third line is: " ++ x

您可以-fwarn-unused-do-bind编译器标志来让ghc标记这些类型的-fwarn-unused-do-bind语句。 在你的例子中,ghc会发出警告:

...: Warning:
    A do-notation statement discarded a result of type ‘Int’
    Suppress this warning by saying ‘_ <- doSomething [1]’
    or by using the flag -fno-warn-unused-do-bind

Since doSomething [1] is [2,1], your code is equivalent to:

afterThreeTurns = do
   first <- ["test"]
   x <- [2,1]
   return first

This is the same as the list comprehension [ first | first <- ["test"], x <- [2,1] ] which explains why you are getting a list of length 2.

Note that the variable x is not referenced anywhere, so this could also be written:

afterThreeTurns = do
   first <- ["test"]
   _ <- [2,1]
   return first

Here is an analogous case using the IO monad. The code:

thirdLine = do
  getLine
  getLine
  x <- getLine
  putStrLn $ "The third line is: " ++ x

is the same as:

thirdLine = do
  _ <- getLine
  _ <- getLine
  x <- getLine
  putStrLn $ "The third line is: " ++ x

You can get ghc to flag these kinds of monadic statements with the -fwarn-unused-do-bind compiler flag. In your example ghc will emit the warning:

...: Warning:
    A do-notation statement discarded a result of type ‘Int’
    Suppress this warning by saying ‘_ <- doSomething [1]’
    or by using the flag -fno-warn-unused-do-bind

相关问答

更多
  • (当然这是一个重复的问题,但我现在找不到一个。) 你学习它是为了学习纯粹的函数式编程,这迫使你以完全不同的方式做许多事情。 你有一种新的思维方式。 无状态编程? 编程没有效果? 一切都很懒惰? 类型推理的疯狂类型系统? 到底什么是monads? 你的思想会被反复吹捧,但最终你会从函数式编程中获得新的视角/技巧,如果没有成熟的Haskell,很难以其他方式获得。 试图具体说明的问题是,试图告诉一个非Haskeller他们将从Haskell学到什么就像试图向一个盲人解释“绿色”的颜色。 (Surely this ...
  • >>=的正式名称是绑定的。 我们还可以将其视为“feed through”,“process by”等。MSDN Channel 9的Brian Benkman称其为“推”(向右或向左)。 为什么绑定? 与let类比。 正如let的变量绑定到评估初始表达式的结果一样,“monadic let”将把它的变量“绑定”到它的输入计算结果上: let a = .... or: .... $>> (\ a -> -- non-recursive "let", as in Lisp, ...
  • 你已经遇到了可怕的Monomorphism限制。 您可以禁用它并获取通用功能。 Prelude> let x = (+) Prelude> :t x x :: Integer -> Integer -> Integer Prelude> :set -XNoMonomorphismRestriction Prelude> let y = (+) Prelude> :t y y :: Num a => a -> a -> a 这里的概念是单形限制将使类型限制为单个(单个)混凝土块。 您可以使用NoMonomo ...
  • 我会从我非常类似的答案中挑剔 (虽然可能不是重复的,因为这个问题没有明确处理let )。 报告给出了从do语法到Haskell内核的完整翻译; 与您的问题相关的部分是: do {e} = e do {e;stmts} = e >> do {stmts} do {let decls; stmts} = let decls in do {stmts} 所以你的代码如下所示: doubleX x = do ...
  • 我认为错误的原因是if 表达式的滥用。 您可以像使用大多数命令式语言中的if 语句那样使用它。 简单地说,一定有else 。 然而,在块中,“没有别的东西”是有意义的,就像if语句没有别的东西。 幸运的是, Control.Monad模块将为您提供一个完全相同的功能: import Control.Monad (when) (...) when (choice=="Y") $ do print "applying the argument" let app ...
  • 由于doSomething [1]是[2,1] ,因此您的代码相当于: afterThreeTurns = do first <- ["test"] x <- [2,1] return first 这与列表理解[ first | first <- ["test"], x <- [2,1] ] [ first | first <- ["test"], x <- [2,1] ] ,这就解释了为什么你得到长度为2的列表。 请注意,变量x在任何地方都没有被引用,所以这也可以写成: afterTh ...
  • 我们来看看类型 。 对于sin和cos我们有: cos, sin :: Floating a => a -> a sum : sum :: Num a => [a] -> a 现在, sum y变成了一个 sum y :: Num a => a 这是一个值,而不是一个函数(你可以将它命名为一个没有参数的函数,但这非常棘手,你还需要name () -> a函数 - 有一个关于这个问题的讨论,但我现在找不到链接 - 康纳尔谈到了这一点)。 无论如何,尝试cos . sum y cos . sum y不会工 ...
  • <$>和<*>具有相同的优先级和左关联性。 $的最低优先级为零。 你可以使用ghci来探索关于它们的信息: λ> :i (<$>) (<$>) :: Functor f => (a -> b) -> f a -> f b -- Defined in ‘Data.Functor’ infixl 4 <$> λ> :i (<*>) class Functor f => Applicative (f :: * -> *) where ... (<*>) :: f (a -> b) -> ...
  • 问题在于你的代码中的lambda范围不太正确。 它应该一直延伸到表达式的结尾,而不仅仅是小计算。 你的代码应该解析为 l >>= (\a -> m >>= (\b -> return (a, b)) 顺便说一句,你可以放下括号,这样会更愉快一些。 l >>= \a -> m >>= \b -> return (a, b) 但这种模糊含义。 如果你想要痛苦的明确,我们可以转换为前缀符号并说 bind a f = a >>= f bind l (\a -> bind m (\b -> return ...
  • 这里的问题与when和either类型有关when而either ContT特有的: when :: forall (m :: * -> *). (Monad m) => Bool -> m () -> m () either :: forall a c b. (a -> c) -> (b -> c) -> Either a b -> c 对于某些monad m ,第二个参数必须是m ()类型。 因此when代码的when行可以像这样修改: when True $ k (Left "Error messa ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)