Search code examples
haskellpersistenceyesod

use runDB in a SubSite Yesod


i want create a post method in a SubSite to create a new entity, i have this 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) 

and the Post method:

   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
               |]

but have the following error

  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) =>

I thing that I need add YesodPersist but i'm not sure how


Solution

  • You need to add the following constraint to the postDemoNewR declaration:

    YesodPersist master => YesodPersistBackend master ~ SqlBackend => …
    

    The first constraint tells master must have persistent abilities while the second constraint tells the backend used for persistent should be an SQL backend.

    You can find something similar in this other question