5
Dog
    name Text
    race Text

getAllDogsR :: Handler Html
getAllDogsR = do
    Dogs<- runDB $ selectList [] [Asc DogName]
    defaultLayout
        [whamlet|
            <ul>
                $forall Entity dogid dog <- Dogs
                    <li>
                        #{show $ unKey (dogid)}
       |]

このコードを実行すると、データベースにあるすべての犬の鍵のリストが次のように取得されます

  • PersistInt64 1
  • PersistInt64 2
  • PersistInt64 3
  • PersistInt64 4

しかし、私が実際に望んでいるのは、次の
ようなキーの純粋な値を表示することです:

  • 1
  • 2
  • 3
  • 4

私の質問は、どうすればこれを達成できるかです。

4

2 に答える 2

2

KeyBackend次のように、最初からキーを抽出する必要があります。

extractKey :: KeyBackend backend entity -> String
extractKey = extractKey' . unKey
  where extractKey' (PersistInt64 k) = show k
        extractKey' _ = ""

あなたは今できるはずです

#{extractKey dogid}
于 2013-11-22T11:00:29.993 に答える
2

変化する

#{show $ unKey (dogid)}

#{toPathPiece dogid}
于 2014-04-06T23:18:29.983 に答える