我目前正在努力寻找以可重用和干净的方式实施警报的方法。我不确定Play的闪烁是否仅用于重定向,但是this post表示有一个使用类Result
实例进行闪烁的解决方法。
变通方法建议为视图使用implicit
参数,例如:
@(clientForm: Form[models.Client])(implicit flash: Flash)
然后简单地做这样的事情:
val flash = play.api.mvc.Flash(Map("error" -> "Please select another id for this client"))
Ok(views.html.clients.new_client(boundForm)(flash))
问题是我的视图中有多个隐式参数,i.d。:
(implicit request: RequestHeader, messages: Messages, flash: Flash)
所以编译器抱怨:
未指定的值参数:flash:Flash,消息:消息
我该如何解决这个问题?
如{删除的答案中所建议的那样隐含flash
并未解决问题。我仍然得到这个编译错误:
模糊的隐含值:特征中的两个方法request2flash 控制器类型(隐式请求: play.api.mvc.RequestHeader)play.api.mvc.Flash和值flash类型 play.api.mvc.Flash匹配预期类型play.api.mvc.Flash
答案 0 :(得分:0)
您不需要传递flash隐式参数,因为通过request2flash方法从请求到闪存发生了隐式转换,如错误所示。
另外,因为您已经通过请求获得隐式Flash,所以您不需要这一行:val flash = play.api.mvc.Flash(Map("error" -> "Please select another id for this client"))
由于隐式转换,您可以将request
参数用作Flash参数。
修复缺少隐式消息的错误:import play.api.i18n.Messages.Implicits._