在子站点中使用 runDB Yesod

use runDB in a SubSite Yesod

我想在 SubSite 中创建一个 post 方法来创建一个新实体,我有这个 AForm

demoForm :: RenderMessage master FormMessage => Maybe Demo -> AForm    (HandlerT master IO) Demo
demoForm   demo = Demo 
            <$> areq textField (fieldSettingsLabel ("fieldone"::T.Text))   (demoFieldOne <$> demo)
            <*> areq intField  (fieldSettingsLabel ("fieldone"::T.Text))   (demoFieldTwo <$> demo)
            <*> areq boolField (fieldSettingsLabel ("fieldThree"::T.Text)) (demoFieldThree <$> demo) 
            <*> areq dayField  (fieldSettingsLabel ("fieldFour"::T.Text))  (demoFieldFour <$> demo) 

和Post方法:

   postDemoNewR :: (Yesod master,RenderMessage master FormMessage) => HandlerT DemoCrud (HandlerT master IO) Html
   postDemoNewR = do
            tp <- getRouteToParent 
            ((result,widget), encoding) <- lift $ runFormPost $ renderBootstrap3 BootstrapBasicForm $ demoForm  Nothing
            case result of
                 FormSuccess demo -> do

                             _ <- lift $  runDB $ insert demo
                             redirect DemoNewR
                 _ -> lift $ defaultLayout $ do
                 let actionR = DemoNewR                
                 [whamlet|
                    <form method=post action=@{tp DemoNewR} encType=#{encoding}>
                          ^{widget}
                    <button .btn .btn-default> default text create
               |]

但出现如下错误

  Could not deduce (YesodPersistBackend master
                  ~ persistent-2.1.3:Database.Persist.Sql.Types.SqlBackend)
from the context (Yesod master, RenderMessage master FormMessage)
  bound by the type signature for
             postDemoNewR :: (Yesod master, RenderMessage master FormMessage) =>

我想我需要添加 YesodPersist 但我不确定如何添加

您需要在 postDemoNewR 声明中添加以下约束:

YesodPersist master => YesodPersistBackend master ~ SqlBackend => …

第一个约束告诉 master 必须具有持久能力,而第二个约束告诉用于持久的后端应该是 SQL 后端。

You can find something similar in this other question