我正在使用Yesod子网站。基本上,它是一个博客。我在将表单附加到处理程序时遇到问题。考虑:
getSubBlogR :: Yesod master
=> YesodPersist master
=> PersistQuery (YesodPersistBackend master (HandlerT master IO))
=> RenderMessage master FormMessage
=> HandlerT Blog (HandlerT master IO) Html
getSubBlogR = lift $ do
articles <- runDB $ selectList [] [Asc ArticleDate]
day <- liftIO $ (utctDay <$> getCurrentTime)
(formWidget, enctype) <- generateFormPost $ (articleForm day)
defaultLayout $ [whamlet|
<div .articles>
$forall Entity _ article <- articles
^{articleWidget article}
|]
就目前而言,这确实是编译的。但我实际上并没有使用formWidget,我真的很想。我想要“喜欢”的东西
getSubBlogR :: Yesod master
=> YesodPersist master
=> PersistQuery (YesodPersistBackend master (HandlerT master IO))
=> RenderMessage master FormMessage
=> HandlerT Blog (HandlerT master IO) Html
getSubBlogR = lift $ do
articles <- runDB $ selectList [] [Asc ArticleDate]
day <- liftIO $ (utctDay <$> getCurrentTime)
(formWidget, enctype) <- generateFormPost $ (articleForm day)
defaultLayout $ [whamlet|
<div .articles>
$forall Entity _ article <- articles
^{articleWidget article}
<div .panel .panel-default>
<div .panel-heading><h1>Add Article
<div .panel-body>
<form method="post" action=@{SubBlogR} enctype=#{enctype}>
^{formWidget}
|]
但是这不能编译。我收到错误:
src/Yesod/Blog/Handler.hs:64:28:
Could not deduce (master ~ Blog)
from the context (Yesod master,
YesodPersist master,
PersistQuery (YesodPersistBackend master (HandlerT master IO)),
RenderMessage master FormMessage)
bound by the type signature for
getSubBlogR
...
Expected type: WidgetT
master IO (Route Blog -> [(Text, Text)] -> Text)
Actual type: WidgetT
master
IO
(Route (HandlerSite (WidgetT master IO)) -> [(Text, Text)] -> Text)
好的,够公平的。我知道“主人”和“博客”的类型并不相同。但是如何让“图表”通勤呢?
答案 0 :(得分:1)
务必为所有表单添加类型注释!这就是Michael Snoyman要求articleForm
类型的原因。我的子网站拒绝类似错误的类型检查,因为我没有注释这个功能:
simpleSourceForm = DataSourceInput
<$> areq textField "Name" Nothing
<*> areq intField "Start" Nothing
<*> areq intField "End" Nothing
这给了我这些错误:
Yesod\DataSource.hs:58:36:
Couldn't match type `m0' with `HandlerT m IO'
because type variable `m' would escape its scope
This (rigid, skolem) type variable is bound by
the type signature for
postDataSourceInputR :: YesodDataSource m =>
HandlerT DataSource (HandlerT m IO) Html
Yesod\DataSource.hs:49:7:
No instance for (RenderMessage (HandlerSite m0) FormMessage)
arising from a use of `areq'
In the second argument of `(<$>)', namely
`areq textField "Name" Nothing'
In the first argument of `(<*>)', namely
`DataSourceInput <$> areq textField "Name" Nothing'
In the first argument of `(<*>)', namely
`DataSourceInput <$> areq textField "Name" Nothing
<*> areq intField "Start" Nothing'
Yesod\DataSource.hs:30:22:
Could not deduce (m ~ HandlerSite m0)
from the context (YesodDataSource m)
bound by the type signature for
getDataSourceInputR :: YesodDataSource m =>
HandlerT DataSource (HandlerT m IO) Html
注释函数修复了所有内容:
simpleSourceForm :: YesodDataSource m => AForm (HandlerT m IO) DataSourceInput
simpleSourceForm = DataSourceInput
<$> areq textField "Name" Nothing
<*> areq intField "Start" Nothing
<*> areq intField "End" Nothing
(我还将包括YesodDataSource类型类用于提供信息)
class (RenderMessage master FormMessage, Yesod master) => YesodDataSource master