scala - 缺少方法 apply 的参数...Play Framework 2.4 编译错误

标签 scala playframework-2.0

Compilation error: missing arguments for method apply in class newPost; follow this method with `_' if you want to treat it as a partially applied function

我不明白模板处理方法必须是什么样子以及编译器对我的要求。
https://github.com/flatlizard/blog

Controller :

  def addPost = Action{ implicit request =>
    Ok(views.html.newPost(postForm))
  }

  def createPost = Action { implicit request =>
    postForm.bindFromRequest.fold(
      hasErrors => BadRequest,
      success => {
        Post.create(Post(new Date, success.title, success.content))
        Ok(views.html.archive("my blog", Post.all))
      })
  }

路线:

GET        /archive/new         controllers.Application.addPost
POST       /archive             controllers.Application.createPost

查看:

@(postForm: Form[Post])(content: Html)(implicit messages: Messages)
@import helper._

@form(routes.Application.createPost) {
    @inputDate(postForm("date"))
    @textarea(postForm("title"))
    @textarea(postForm("content"))
    <button id="submit" type="submit" value="Submit" class="btn btn-primary">Submit</button>
}

更新

我解决了在 Controller 文件中添加以下导入的问题:

import play.api.i18n.Messages.Implicits._
import play.api.Play.current

请参阅 Play 2.4 迁移: https://www.playframework.com/documentation/2.4.x/Migration24#I18n

最佳答案

编译错误

您的 View 需要传递三个参数,但您只传递了一个。要解决编译错误,请将 View 中的签名从 @(postForm: Form[Post])(content: Html)(implicit messages: Messages) 更改为至@(postForm: Form[Post])(implicit messages: Messages) .

参数和 View

示例中的第二个参数 (content: Html)当您组合多个 View 时使用:

index.scala.html

@(text: String)

@main("Fancy title") {
  <div>@text</div>
}

ma​​in.scala.html

@(title: String)(content: Html)

<html>
  <head>
    <title>@title</title>
  </head>
  <body>
    @content
  <body>
</html>

调用 Controller

Ok(views.html.index("Some text"))

在此示例中,您将“一些文本”传递到索引 View 。然后索引 View 调用主视图并传递另外两个参数。 titlecontent ,其中content是index.scala.html 大括号之间的html ( <div>@text</div> )

最后隐含参数:(implicit messages: Messages)必须位于要隐式传递到您的 View 的范围内的某个位置。例如,在您的 Controller 中如下所示:

def addPost = Action{ implicit request =>
   implicit val messages: Messages = ...
   Ok(views.html.newPost(postForm))
}

关于scala - 缺少方法 apply 的参数...Play Framework 2.4 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30760714/

相关文章:

scala - 组合序列元素的最简洁方式

playframework - intellij play framework 2.0.2 配置

syntax - Scala "magic"函数列表

scala - Apache Spark : distinct doesnt work?

playframework - 无法找到 play.filters 包

playframework-2.0 - 如何在 play2.0 中操作 Session、Request 和 Response 进行测试

playframework-2.0 - 使用 Mesos 管理 Web App + 数据库集群

playframework-2.0 - Play 2.0 应用程序的依赖管理

scala - 在 Play Framework 2.4.2 中使用 scalates/specs2 测试 HttpErrorHandler

scala - 如何在 SBT 1.0 中更改项目的 ID?