Search code examples
scalasitemaplift

Lift: Menu with multiple params [Menu.params]


How to build an url with two or many params?

i've a case class:

case class PageDetail(index: String, id: String)

i'm trying to do a menu val:

val menu = Menu.params[(String,String)]( "pageDetail", "Page Detail",
    ids => { case Full(index) :: Full(id) :: Nil => Full((index, id))},
    pi => { case (index, id) => index :: id :: Nil }) / "admin" / "detail"

i would like to obtain a link as .../admin/detail/indexxxxxxx/idddddddddd where indexxxxxxx and idddddddddd are my params. as is doesn't work. Error in compile time. How can i do? Thanks


Solution

  • Most likely, the issue is in your extractor pattern. When you are matching on your list here:

    case Full(index) :: Full(id) :: Nil => Full((index, id))
    

    The parameters are always going to be defined, so the Full is not possible. You can use functions, such as AsInt to require the parameter to be an Int, or else it will look for a String. You'd most likely want to start with the following (Or some variation on that):

    case index :: id :: Nil => Full((index, id))
    

    If you are using Empty to mean the parameter is optional, then you would simply add a second case statement after it with the parameter omitted.

    Also, you probably need to add / ** to the end of you / "admin" / "detail" mapping so it knows to grab the parameters from there.

    So, the code should look something like this:

    val menu = Menu.params[(String,String)]( "pageDetail", "Page Detail",
      { 
        case index :: id :: Nil => Full((index, id))
      }, { 
        case (index, id) => index :: id :: Nil 
      }
    ) / "admin" / "detail" / **