【问题标题】:Yesod Subsites and FormsYesod 子网站和表格
【发布时间】:2014-06-04 23:48:47
【问题描述】:

我正在开发一个 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)

好的,很公平。我知道“大师”和“博客”不是同一类型。但是如何让“图表”通勤?

【问题讨论】:

  • articleForm的类型是什么?

标签: haskell yesod


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 2015-08-02
    • 1970-01-01
    • 2015-07-27
    • 2023-03-07
    相关资源
    最近更新 更多