Search code examples
scalaplayframeworkplayframework-2.2

Can't use Flash in a view


I have a main-layout page:

//layouts/main.scala.html
@(title: String)(content: => Html)(implicit flash: Flash)
<!DOCTYPE html>
<html>
    <!-- .... -->
    <body>
        <h2>Here is the flash:</h2>
        @views.html.layouts._flash(flash)
        <section class="content">My super content: @content</section>
    </body>
</html>

//layouts/_flash.scala.html
@(flash: Flash)
@flash.data.foreach { case (k, v) =>
    key, value: (@k, @v)
}

And a controller:

//controllers/Application.scala
  def index = Action { implicit request =>
    Ok(views.html.application.index())
  }

And a view for it:

//application/index.scala.html
@layouts.main("Index") {
    <h1>Index page</h1>
}

The view of index.scala.html throws an error:

could not find implicit value for parameter flash: play.api.mvc.Flash

I tried this:

//application/index.scala.html
@(implicit flash: Flash)
@layouts.main("Index") {
    <h1>Index page</h1>
}

And it caused another error:

not enough arguments for method apply: (implicit flash: play.api.mvc.Flash)play.api.templates.HtmlFormat.Appendable in object index. Unspecified value parameter flash.

Solution

  • Try to change your view to this:

    @()(implicit flash: Flash)
    @layouts.main("Index") {
        <h1>Index page</h1>
    }
    

    or change your controller to this:

    def index = Action { implicit request =>
        Ok(views.html.application.index(request.flash))
    }