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))
    }
}
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With