Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flash not found in Products.scala

I am currently reading 'Play for Scala' by Peter Hilton. I have just got the the end of the first example Play app where you build a paperclip directory.

However upon compiling I get a compilation error telling me that the value 'flash' has not been found. Usually this is a simple mistake I have made but given that I am just following the guide in the book I can't identify a fix.

The error is in lines 52 and 53 in the 'NewProduct' function

Here is the code:

package controllers

import play.api.mvc.{Action, Controller}
import models.Product
import play.api.data.Form
import play.api.data.Forms.{mapping, longNumber, nonEmptyText}
import play.api.i18n.Messages
import play.api.mvc.Flash

object Products extends Controller {

    private val productForm: Form[Product] = Form(
        mapping(
            "ean" -> longNumber.verifying(
                "validation.ean.duplicate", Product.findByEan(_).isEmpty),
            "name" -> nonEmptyText,
            "description" -> nonEmptyText
            )(Product.apply)(Product.unapply)
        )

    def list = Action {implicit request =>
        val products = Product.findAll

        Ok(views.html.products.list(products))
    }

    def show(ean: Long) = Action {implicit request =>
        Product.findByEan(ean).map {product =>
            Ok(views.html.products.details(product))
        }.getOrElse(NotFound)
    }

    def save = Action { implicit request =>
        val newProductForm = productForm.bindFromRequest()

        newProductForm.fold(
            hasErrors = {form =>
                Redirect(routes.Products.newProduct()).
                    flashing(Flash(form.data) + ("error" -> Messages("validation.errors")))
            },

            success = {newProduct =>
                Product.add(newProduct)
                val message = Messages("products.new.success", newProduct.name)
                Redirect(routes.Products.show(newProduct.ean)).
                    flashing("success" -> message)
            }
        )
    }

    def newProduct = Action { implicit request =>
        val form = if(flash.get("error").isDefined)
            productForm.bind(flash.data)
        else
            productForm

        Ok(views.html.products.editProduct(form))
    }
}
like image 487
Chris Brown Avatar asked Aug 09 '14 11:08

Chris Brown


1 Answers

Example is working with Play < 2.3, you may want to check which version you are currently using. With Play > 2.3, request.flash must be used instead. In both case you can use request.flash (which is more explicit).

like image 151
cchantep Avatar answered Nov 02 '22 15:11

cchantep