在Products.scala中找不到Flash [英] flash not found in Products.scala

查看:72
本文介绍了在Products.scala中找不到Flash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在阅读彼得·希尔顿(Peter Hilton)的"Play for Scala".我刚刚结束了第一个示例Play应用的结尾,您在其中构建了一个回形针目录.

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.

但是,编译时出现编译错误,告诉我未找到值"flash".通常,这是我犯的一个简单错误,但是由于我只是按照书中的指南进行操作,因此无法确定修复方法.

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.

错误在"NewProduct"功能的第52和53行中

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

这是代码:

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))
    }
}

推荐答案

示例正在与Play< 2.3,您可能要检查当前使用的版本.对于Play> 2.3,必须改为使用request.flash.在这两种情况下,您都可以使用request.flash(更明确).

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).

这篇关于在Products.scala中找不到Flash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆