scalaplayframework-2.1flash-scope

Play2 and Flash Scope, why can't I print my message?


In my current Controller I'm passing a flash message as described in the docs

Controller:

def test(token: String) = Action {
        Ok(views.html.mobile.smsReview(smsReviewForm.fill(model), grades, smstoken.get.token))
        .flashing("success" -> Messages("sms.form.write.review"))
      }
  }

my view:

@(smsReviewForm: Form[SmsReview], grades: Seq[Grade], smstoken: String)(implicit request: RequestHeader)

...

@request.flash.get("success").getOrElse("HELLO!").map { msg =>
        <div class="alert alert-info">
            @msg
        </div>
    }

In the view HELLO! is printed, not my message. But if I check the headers in Chrome my message is there:

Content-Length:3596
Content-Type:text/html; charset=utf-8
Set-Cookie:PLAY_FLASH=success%3AHej%21+Skriv+din+rekommendation+nedan; Path=/; HTTPOnly

What have I missed?


Solution

  • The flash scope is used to 'flash' a message to the next request. This is used mostly when you are redirecting to another page. The most common use case is the redirect at the end of a form post.

    The first part of the documentation states this:

    If you have to keep data across multiple HTTP requests, you can save them in the Session or Flash scopes. Data stored in the Session are available during the whole user Session, and data stored in the Flash scope are available to the next request only.

    In your case you could simply pass the message directly to the view as you have message available at the time you render the view.