使用Play with Scala从表单中保存模型

时间:2015-11-12 02:40:40

标签: scala playframework

我正在尝试使用form中的数据创建并保存新的 Supply 模型。

以下是 SuppliesController 的相关代码:

  def submitSupplyOffer = SecuredAction.async { implicit request =>
    SupplyForm.form.bindFromRequest.fold(
      form => Future.successful(BadRequest(views.html.supplies.index(request.identity, form))),
      data => {
        val supply = Supply(
          id = UUID.randomUUID(),
          userID = request.identity.userID,
          resource = data.resource,
          amount = data.amount
        )
        for {
          supply <- supplyService.save(supply.copy())
          result <- Redirect(routes.Application.index())
        } yield {
          result
        }
      }
    )
  }

这是我得到的错误:

enter image description here

提前致谢!

1 个答案:

答案 0 :(得分:1)

Redirect.apply会返回Result,而不是Future[Result],因此您需要将其从for-comprehension中删除:

  def submitSupplyOffer = SecuredAction.async { implicit request =>
    SupplyForm.form.bindFromRequest.fold(
      form => Future.successful(BadRequest(views.html.supplies.index(request.identity, form))),
      data => {
        val supply = Supply(
          id = UUID.randomUUID(),
          userID = request.identity.userID,
          resource = data.resource,
          amount = data.amount
        )
        for {
          supply <- supplyService.save(supply.copy())
        } yield Redirect(routes.Application.index())
      }
    )
  }