0
votes

How to construct reverse routes in (Scala) Play 2.4 outside controllers? If I import controllers._ and try to use routes, I get a compilation error:

Cannot find any HTTP Request Header here

package models 

import org.joda.time._ 
import play.api.libs.json._ 

case class Attendee (id: Long, status: Boolean, ... , pictureUrl: Option[String], ...) 

object Attendee { implicit val attendeeWrites = new Writes[Attendee] { 

def writes(att: Attendee) = Json.obj( "id" -> att.id, "picture" -> att.pictureUrl.map 
    { somePictureUrl => routes.PrivateWS.getAttendeeImage(att.eventId, att.id)
    .absoluteURL() }) 
1
What exactly do you want to do? Can you share a small example?Igor Mielientiev
It cant find an implicit RequestHeader. Have you done the relevant 'imports' and 'extend's?airudah
Based on "standard" directory layout of basic play applications, I have controllers and models packages in app directory. One of model members looks like: package models import org.joda.time._ import play.api.libs.json._ case class Attendee (id: Long, status: Boolean, ... , pictureUrl: Option[String], ...) object Attendee { implicit val attendeeWrites = new Writes[Attendee] { def writes(att: Attendee) = Json.obj( "id" -> att.id, "picture" -> att.pictureUrl.map { somePictureUrl => routes.PrivateWS.getAttendeeImage(att.eventId, att.id).absoluteURL() })Michal Pitonak
The problem exactly is that I don't know what to import or extendMichal Pitonak
import play.api.mvc.Request, and: \n def writes(att: Attendee)(implicit request: Request[_]) = Json.obj( "id"...airudah

1 Answers

0
votes

I have create an helper and use controllers.route.etc etc and works well,

in your case you can try

controllers.routes.PrivateWS.getAttendeeImage(att.eventId, att.id).absoluteURL()