如何按ID删除项目并重定向?

时间:2022-08-22 20:32:42

I would like to make a function which deletes a item by id and redirects to user an other page.

我想创建一个按id删除项目并重定向到用户其他页面的功能。

deleteItemR :: ItemId -> Html deleteItemR itemid = runDB $ delete itemid redirect ItemR

deleteItemR :: ItemId - > Html deleteItemR itemid = runDB $ delete itemid redirect ItemR

I tried so many times in other ways but it did not work as well.

我在其他方面尝试了很多次,但它没有用。

1 个解决方案

#1


Your code should look like this:

您的代码应如下所示:

deleteItemR :: ItemId -> Handler Html
deleteItemR itemid = do
    runDB $ get404 itemid
    runDB $ delete itemid
    setMessage . toHtml $ ("Item deleted" :: Text)
    redirectUltDest ItemR

First, your function signature cannot be ItemID -> Html, it must be executed in a Handler.

首先,您的函数签名不能是ItemID - > Html,它必须在Handler中执行。

You also need to use the do notation to emulate a sequence of actions in Haskell.

您还需要使用do notation来模拟Haskell中的一系列操作。

Note that any action in such a sequence can end the execution. For example, the runDB $ get404 itemid will try to fetch the item referenced by itemid. If it does not exist, the user will be redirected to the 404 page (not found) without trying to execute the other actions.

请注意,此类序列中的任何操作都可以结束执行。例如,runDB $ get404 itemid将尝试获取itemid引用的项。如果它不存在,则用户将被重定向到404页面(未找到)而不尝试执行其他操作。

We need the runDB $ get404 itemid action because the runDB $ delete itemid action will not end the sequence even if the item does not exist. It would therefore indicate an item was deleted though there was none.

我们需要runDB $ get404 itemid操作,因为即使该项不存在,runDB $ delete itemid操作也不会结束序列。因此,它表示虽然没有项目被删除。

#1


Your code should look like this:

您的代码应如下所示:

deleteItemR :: ItemId -> Handler Html
deleteItemR itemid = do
    runDB $ get404 itemid
    runDB $ delete itemid
    setMessage . toHtml $ ("Item deleted" :: Text)
    redirectUltDest ItemR

First, your function signature cannot be ItemID -> Html, it must be executed in a Handler.

首先,您的函数签名不能是ItemID - > Html,它必须在Handler中执行。

You also need to use the do notation to emulate a sequence of actions in Haskell.

您还需要使用do notation来模拟Haskell中的一系列操作。

Note that any action in such a sequence can end the execution. For example, the runDB $ get404 itemid will try to fetch the item referenced by itemid. If it does not exist, the user will be redirected to the 404 page (not found) without trying to execute the other actions.

请注意,此类序列中的任何操作都可以结束执行。例如,runDB $ get404 itemid将尝试获取itemid引用的项。如果它不存在,则用户将被重定向到404页面(未找到)而不尝试执行其他操作。

We need the runDB $ get404 itemid action because the runDB $ delete itemid action will not end the sequence even if the item does not exist. It would therefore indicate an item was deleted though there was none.

我们需要runDB $ get404 itemid操作,因为即使该项不存在,runDB $ delete itemid操作也不会结束序列。因此,它表示虽然没有项目被删除。