编译时出现Yesod错误

时间:2015-06-02 08:00:19

标签: haskell yesod

对于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 :: Handler 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 个答案:

答案 0 :(得分:0)

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功能使用了相同的模板。对于那些函数,你应该初始化相同的值。