2014-04-20 42 views
2

我正在使用Yesod子網站。基本上它是一個博客。我在處理程序中附加表單時遇到問題。考慮: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,我真的很想。我想要「like」

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" [email protected]{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

'articleForm'的類型是什麼? –

回答

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