这是我的问题的后续行动:Extracting database field values inside a Handler
我想从数据库中提取一些信息并将其作为表单的标签值传递。但是,我收到类型错误。
下面是一个简单的演示(来自 Yesod Book 的 shell 代码):
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
import Control.Applicative ((<$>), (<*>))
import Data.Text (Text)
import Data.Time (Day)
import Yesod
import Yesod.Form.Jquery
data App = App
mkYesod "App" [parseRoutes|
/ HomeR GET
|]
instance Yesod App
instance RenderMessage App FormMessage where
renderMessage _ _ = defaultFormMessage
instance YesodJquery App
data Person = Person
{ personName :: Text
}
deriving Show
personForm :: Text -> Html -> MForm Handler (FormResult Person, Widget)
personForm n1 = renderDivs $ Person
<$> areq textField n1 Nothing -- Changing n1 to "Name" works just fine.
getHomeR :: Handler Html
getHomeR = do
(widget, enctype) <- generateFormPost $ personForm "test"
defaultLayout
[whamlet|
<p>
The widget generated contains only the contents
of the form, not the form tag itself. So...
|]
main :: IO ()
main = warp 3000 App
当我使用 runhaskell 运行程序时,出现以下错误:
Couldn't match expected type `FieldSettings site0'
with actual type `Text'
In the second argument of `areq', namely `n1'
In the second argument of `(<$>)', namely
`areq textField n1 Nothing'
In the second argument of `($)', namely
`Person <$> areq textField n1 Nothing'
我也试过 (FieldSettings n1 Nothing Nothing Nothing [])
但没有运气。关于如何将标签值传递给 areq 的任何想法?