crockpotveggies
crockpotveggies

Reputation: 13300

Type mismatch when using Scala Trait in Play?

Using the Play! Framework 1.2.4. I've got a nifty trait that checks an API key and HTTPS, but if I want to access the account associated with that key and reference it in my controller, it throws a type mismatch; found : java.lang.Object required: Long

So here's my API controller (incomplete):

object API extends Controller with Squeryl with SecureAPI {

  import views.API._

  def job(param:String) = {


    val Job = models.Job
    param match {
      case "new"    => Job.createFromParams(params,thisAccount) //thisAccount comes from the trait
      case "update" =>
      case "get"    =>
      case "list"   =>
    }
  }

}

and the secure trait:

trait SecureAPI {
  self:Controller =>

  @Before
  def checkSecurity(key:String) = {
      if(!self.request.secure.booleanValue) {
          Redirect("https://" + request.host + request.url);
      } else {
          models.Account.getByKey(key) match {
            case Some(account)  =>  {
              renderArgs += "account" -> account.id
              Continue
            }
            case _  =>  Forbidden("Key is not authorized.")
          }
      }
  }

  def thisAccount = renderArgs("account").get
}

How would I properly access thisAccount? Thanks

Upvotes: 1

Views: 270

Answers (1)

Andrzej Doyle
Andrzej Doyle

Reputation: 103777

Your problem is simply that renderArgs is only declared to return an Object from its get call (which is fair enough because it could be just about anything).

Consequently the inferred type of your thisAccount method will be () => Object.

You'll need to cast the returned type into a Long, something like (though perhaps with some error-checking):

def thisAccount = renderArgs("account").get.asInstanceOf[Long]

Upvotes: 2

Related Questions