BARJ
BARJ

Reputation: 2002

yesod how to show the pure value of a PersistInt64 key

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

When I run this code I will get a list of all dog keys that are in my database
like this:

but what I actually want is to show the pure value of the key
like this:

My question is how can I achieve this.

Upvotes: 5

Views: 554

Answers (2)

user316146
user316146

Reputation: 1307

Change

#{show $ unKey (dogid)}

to

#{toPathPiece dogid}

Upvotes: 2

Sammy S.
Sammy S.

Reputation: 1290

You need to extract the key out of a KeyBackend first, like so:

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

You should now be able to do

#{extractKey dogid}

Upvotes: 2

Related Questions