在SubSite Yesod中使用runDB

时间:2015-05-29 14:13:18

标签: haskell persistence 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,但我不确定如何

1 个答案:

答案 0 :(得分:0)

您需要将以下约束添加到postDemoNewR声明:

YesodPersist master => YesodPersistBackend master ~ SqlBackend => …

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

You can find something similar in this other question