【问题标题】:missing arguments for method apply... Play Framework 2.4 compilation error方法应用缺少参数... Play Framework 2.4 编译错误
【发布时间】:2015-06-13 03:58:54
【问题描述】:

编译错误:在类 newPost 中应用方法缺少参数; 如果您想将其视为部分应用的函数,请在此方法后面加上 `_'

我不明白模板处理方法的外观以及编译器对我的要求。
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

【问题讨论】:

    标签: scala playframework-2.0


    【解决方案1】:

    编译错误

    您的视图希望传递三个参数,但您只传递了一个。要解决您的编译错误,请将您视图中的签名从@(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>
    }
    

    ma​​in.scala.html

    @(title: String)(content: Html)
    
    <html>
      <head>
        <title>@title</title>
      </head>
      <body>
        @content
      <body>
    </html>
    

    调用控制器

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

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

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

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

    【讨论】:

    • 感谢您的解释,但我仍然不明白为什么 Play 要求我的模板在参数中包含 play.api.i18n.Messages。
    • 据我所知,Messages 不是必需的。您不在模板中使用它。不能只删除视图中的参数吗?
    • 当我删除它时 Play 抛出编译错误:Error:(7, 20) Play 2 Compiler: blog\app\views\newPost.scala.html:7: could not find implicit value for parameter messages : play.api.i18n.Messages @textarea(postForm("content")) ^
    • 我的错,对不起。助手游戏需要Messages。请参阅此处的apply 方法,例如:playframework.com/documentation/2.4.x/api/scala/…。因此,您的视图文件中的正确签名是 @(postForm: Form[Post])(implicit messages: Messages)
    【解决方案2】:

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

    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

    【讨论】:

      猜你喜欢
      • 2015-08-24
      • 2021-07-24
      • 2015-06-22
      • 1970-01-01
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      相关资源
      最近更新 更多