Yesod 编译时出错
Yesod errors when compiling
对于 AForm,我遇到了一些编译错误:
1.对于默认 Int 值:
data Person = Person
{ name :: Text
, age :: Maybe Int
}
deriving Show
formPerson :: AForm Handler Person
formPerson = Person
<$> areq textField "Person Name" Nothing
<*> aopt intField "Person Age" (Just 25)
错误信息:
No instance for (Num (Maybe Int)) arising from the literal ‘25’
In the first argument of ‘Just’, namely ‘25’
In the third argument of ‘aopt’, namely ‘(Just 25)’
In the second argument of ‘(<*>)’, namely
‘aopt intField "Person Age" (Just 25)’
2。模板错误:
getPersonR :: 处理程序 Html
getPersonR = do
let myAge = 25::Int
defaultLayout $ do
$(widgetFile "persone")
在模板处:
<span>My age: #{myAge}
结束错误:
Not in scope: ‘myAge’
In the splice: $(widgetFile "persone")
3。结果错误:
postPersonR :: Handler Html
postPersonR = do
((result, formWidget), formEnctype) <- runFormPost formPerson
let title = "Success!"::Html
case result of
FormSuccess person ->
defaultLayout $ do
setTitle title
$(widgetFile "personresult")
_ ->
defaultLayout $ do
$(widgetFile "person")
和模板:
<strong>Your name: #{name person}
错误:
Couldn't match expected type ‘Person -> a0’
with actual type ‘blaze-markup-0.6.1.1:Text.Blaze.Internal.MarkupM
()’
The function ‘name’ is applied to one argument,
but its type ‘Html’ has none
In the first argument of ‘toHtml’, namely ‘name person’
In the first argument of ‘asWidgetT GHC.Base.. toWidget’, namely
‘toHtml (name person)’
如何解决问题?
1 - 双示例 - 为字段和整个 personeForm 设置默认值:
data Person = Person
{ name :: Text
, age :: Maybe Int
}
personeForm :: Maybe Person -> AForm Handler Person
personeForm person = Person
<$> areq textField "Persone Name" (name <$> person)
<*> aopt intField "Persone Age" (Just (Just 24))
getAformPersonR :: Handler Html
getAformPersonR = do
let person = Person "Jack" Nothing
(formWidget, formEnctype) <- generateFormPost $ renderDivs $ personeForm $ Just person
defaultLayout $ do
$(widgetFile "aformpersone")
2,3 - 不在范围内:
您对 Get ang Post 函数使用了相同的模板。对于这些功能,您应该初始化相同的值。
对于 AForm,我遇到了一些编译错误:
1.对于默认 Int 值:
data Person = Person
{ name :: Text
, age :: Maybe Int
}
deriving Show
formPerson :: AForm Handler Person
formPerson = Person
<$> areq textField "Person Name" Nothing
<*> aopt intField "Person Age" (Just 25)
错误信息:
No instance for (Num (Maybe Int)) arising from the literal ‘25’
In the first argument of ‘Just’, namely ‘25’
In the third argument of ‘aopt’, namely ‘(Just 25)’
In the second argument of ‘(<*>)’, namely
‘aopt intField "Person Age" (Just 25)’
2。模板错误: getPersonR :: 处理程序 Html
getPersonR = do
let myAge = 25::Int
defaultLayout $ do
$(widgetFile "persone")
在模板处:
<span>My age: #{myAge}
结束错误:
Not in scope: ‘myAge’
In the splice: $(widgetFile "persone")
3。结果错误:
postPersonR :: Handler Html
postPersonR = do
((result, formWidget), formEnctype) <- runFormPost formPerson
let title = "Success!"::Html
case result of
FormSuccess person ->
defaultLayout $ do
setTitle title
$(widgetFile "personresult")
_ ->
defaultLayout $ do
$(widgetFile "person")
和模板:
<strong>Your name: #{name person}
错误:
Couldn't match expected type ‘Person -> a0’
with actual type ‘blaze-markup-0.6.1.1:Text.Blaze.Internal.MarkupM
()’
The function ‘name’ is applied to one argument,
but its type ‘Html’ has none
In the first argument of ‘toHtml’, namely ‘name person’
In the first argument of ‘asWidgetT GHC.Base.. toWidget’, namely
‘toHtml (name person)’
如何解决问题?
1 - 双示例 - 为字段和整个 personeForm 设置默认值:
data Person = Person
{ name :: Text
, age :: Maybe Int
}
personeForm :: Maybe Person -> AForm Handler Person
personeForm person = Person
<$> areq textField "Persone Name" (name <$> person)
<*> aopt intField "Persone Age" (Just (Just 24))
getAformPersonR :: Handler Html
getAformPersonR = do
let person = Person "Jack" Nothing
(formWidget, formEnctype) <- generateFormPost $ renderDivs $ personeForm $ Just person
defaultLayout $ do
$(widgetFile "aformpersone")
2,3 - 不在范围内:
您对 Get ang Post 函数使用了相同的模板。对于这些功能,您应该初始化相同的值。