我正在尝试使用Yesod构建一个简单的网站,我开始使用Max Tagher在Youtube上的优秀介绍,YesodScreencast的代码。我已经从GitHub分叉了他的代码,我想在帖子中添加一个日期来表明它的发布时间,但是我遇到的问题是我无法理解的问题Haskell的经验和初学者对Yesod的体验。我一直无法通过Googleplex找到答案。
Yesod在dayField
中提供了原生Yesod.Form.Fields
,因此我认为我需要做的就是使用{{1}将以下Blog postdate Field
添加到配置/模型中}:
Day
并将其添加到BlogPost
title Text
postdate Day
article Markdown
中的blogPostForm
:
PostNew.hs
当编译时,我收到以下错误消息:
blogPostForm :: AForm Handler BlogPost
blogPostForm = BlogPost
<$> areq textField (bfs ("Title" :: Text)) Nothing
<*> areq dayField (bfs ("Postdate" :: Day)) Nothing
<*> areq markdownField (bfs ("Article" :: Text)) Nothing
如果我将Handler/Home.hs:16:11:
Could not deduce (blaze-markup-0.6.3.0:Text.Blaze.ToMarkup Day)
arising from a use of ‘toHtml’
from the context (PersistEntity BlogPost)
bound by a pattern with constructor
Entity :: forall record.
PersistEntity record =>
Key record -> record -> Entity record,
in a lambda abstraction
at Handler/Home.hs:16:11-34
In the first argument of ‘asWidgetT GHC.Base.. toWidget’, namely
‘toHtml (blogPostPostdate post_apZp)’
In a stmt of a 'do' block:
(asWidgetT GHC.Base.. toWidget)
(toHtml (blogPostPostdate post_apZp))
In the expression:
do { (asWidgetT GHC.Base.. toWidget)
((blaze-markup-0.6.3.0:Text.Blaze.Internal.preEscapedText
GHC.Base.. Data.Text.pack)
"<h4><li><a href=\"");
(getUrlRenderParams
>>=
(\ urender_apZq
-> (asWidgetT GHC.Base.. toWidget)
(toHtml
(\ u_apZr -> urender_apZq u_apZr [] (PostDetailsR id_apZo)))));
(asWidgetT GHC.Base.. toWidget)
((blaze-markup-0.6.3.0:Text.Blaze.Internal.preEscapedText
GHC.Base.. Data.Text.pack)
"\">");
(asWidgetT GHC.Base.. toWidget)
(toHtml (blogPostPostdate post_apZp));
.... }
更改为Day
,一切都会按预期进行。我不确定为什么Yesod无法处理Text
,因为Day
中有dayField
我希望处理此问题。我觉得这很简单,但我似乎无法确定修复此错误需要做些什么。
答案 0 :(得分:1)
Date
数据类型似乎没有ToMarkup的实例。
您可以自己提供一个实例:
instance ToMarkup Date where
toMarkup = toMarkup . show
将日期转换为字符串,然后将其转换为标记。如果默认的show实例不符合您的需求,您可以自己提供格式化程序并将其放在show
的位置。