Frage

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?

War es hilfreich?

Lösung

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.

Andere Tipps

From the HTTP Cookie page on Wikipedia

Set-Cookie is a directive for the browser to store the cookie and send it back in future requests to the server...

Seeing the SET_COOKIE header in the response indicates that subsequent requests you make to the server will send the cookie and views rendered will have access to the data in the flash scope (provided you add (implicit flash: Flash) to the template function definition).

As EECOLOR stated above you simply need to pass the data to your view as a parameter. There is no need in this case to use the flash scope.

Below I have adapter your code to pass the message String to the view as a parameter.

Controller

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

View

@(smsReviewForm: Form[SmsReview], grades: Seq[Grade], smstoken: String, message: String)(implicit request: RequestHeader)
...
<div class="alert alert-info">@message</div>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top