我在Yesod的SubSite中创建CRUD操作有很多问题,我有以下路由
mkYesodSubData "DemoCrud" [parseRoutes|
/demoNew DemoNewR GET POST
/demoEdit/#DemoId DemoEditR GET POST
|]
以及以下subDispach实例
instance (Yesod master,RenderMessage master FormMessage, YesodPersist master, YesodPersistBackend master ~ SqlBackend)
=> YesodSubDispatch DemoCrud (HandlerT master IO)
where
yesodSubDispatch = $(mkYesodSubDispatch resourcesDemoCrud)
但有此错误
Couldn't match expected type ‘DemoId
-> HandlerT DemoCrud (HandlerT parent1 IO) a0’
with actual type ‘HandlerT DemoCrud (HandlerT master0 IO) Html’
我是搜索,但在页面示例中,不要在路径中使用Id
答案 0 :(得分:1)
getDemoEditR :: YesodPersist master
=> YesodPersistBackend master ~ SqlBackend
=> (Yesod master,RenderMessage master FormMessage)
=> DemoId -> HandlerT DemoCrud (HandlerT master IO) Html
getDemoEditR demoId = do
demo <- lift $ runDB $ get404 demoId
(widget, encoding) <- lift $ generateFormPost $ renderBootstrap3 BootstrapBasicForm $ demoForm (Just demo)
lift $ defaultLayout $ do
[whamlet|
<form method=post>
^{widget}
<div>
<input type=submit>
|]
postDemoEditR :: YesodPersist master
=> YesodPersistBackend master ~ SqlBackend
=> (Yesod master,RenderMessage master FormMessage)
=> DemoId -> HandlerT DemoCrud (HandlerT master IO) Html
postDemoEditR demoId = do
demo <- lift $ runDB $ get404 demoId
((result,widget), encoding) <- lift $ runFormPost $ renderBootstrap3 BootstrapBasicForm $ demoForm (Just demo)
case result of
FormSuccess demoResult -> do
_ <- lift $ runDB $ replace demoId demoResult
redirect DemoNewR
_ -> lift $ defaultLayout $ do
[whamlet|
<form method=post>
^{widget}
<div>
<input type=submit>
|]