missing arguments for method apply... Play Framework 2.4 编译错误

missing arguments for method apply... Play Framework 2.4 compilation error

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

控制器:

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

更新

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

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

查看 Play 2.4 迁移: https://www.playframework.com/documentation/2.4.x/Migration24#I18n

编译错误

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

参数和视图

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

index.scala.html

@(text: String)

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

main.scala.html

@(title: String)(content: Html)

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

在控制器中调用

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

在此示例中,您将 "Some text" 传递给索引视图。索引视图然后调用主视图传递另外两个参数。 titlecontent,其中 content 是 index.scala.html (<div>@text</div>)

大括号之间的 html

最后是隐式参数:(implicit messages: Messages) 必须在范围内的某处才能隐式传递给您的视图。例如在你的控制器中像这样:

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

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

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

查看 Play 2.4 迁移:https://www.playframework.com/documentation/2.4.x/Migration24#I18n

更新

实际上这是一个糟糕的方法,因为这里使用了 Play.current,它很快就会被弃用。这是另一个使用依赖注入的解决方案:

路线:

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

控制器:

class Application @Inject() (val messagesApi: MessagesApi)
  extends Controller with I18nSupport{
 ...
}

https://www.playframework.com/documentation/2.4.x/ScalaDependencyInjection