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 aforce_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
最满意答案
由于
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 ...
-
Haskell monads:“(>> =)”和“(= <<)”的名字是什么?(Haskell monads: What is the name for what `(>>=)` and `(=<<)` do?)[2023-07-15]
>>=的正式名称是绑定的。 我们还可以将其视为“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 ...
-
在Haskell中嵌套`do`块(Nested `do` blocks in Haskell)[2021-04-26]
我认为错误的原因是if 表达式的滥用。 您可以像使用大多数命令式语言中的if 语句那样使用它。 简单地说,一定有else 。 然而,在块中,“没有别的东西”是有意义的,就像if语句没有别的东西。 幸运的是, Control.Monad模块将为您提供一个完全相同的功能: import Control.Monad (when) (...) when (choice=="Y") $ do print "applying the argument" let app ... -
与Haskell混淆块(Confusion with Haskell do blocks)[2022-12-20]
由于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 ...