方法应用的参数太多:(汽车:play.api.data.Form[models.CarroFormData])

Posted

技术标签:

【中文标题】方法应用的参数太多:(汽车:play.api.data.Form[models.CarroFormData])【英文标题】:too many arguments for method apply: (car: play.api.data.Form[models.CarroFormData]) 【发布时间】:2016-01-23 18:00:03 【问题描述】:

我在下面遇到此错误,找不到解决方案或如何调试传递给应用的内容。 有人可以帮忙吗?

方法应用的参数太多:(汽车: play.api.data.Form[models.CarroFormData])(隐式消息: play.api.i18n.Messages)play.twirl.api.htmlFormat.Appendable 在类 索引

控制器表单

def add = Action  implicit request =>
    CarroForm.form.bindFromRequest.fold(
      // if any error in submitted data
      errorForm => Ok(views.html.admin.index(errorForm, Seq.empty[Carro])),
      data => repo.create(carro.name, carro.description, carro.img, carro.keywords).map  _ =>
          // If successful, we simply redirect to the index page.
          Redirect(routes.application.index)
        )
  

这是模型

package dal

import javax.inject. Inject, Singleton 
import play.api.db.slick.DatabaseConfigProvider
import slick.driver.JdbcProfile

import models._

import scala.concurrent. Future, ExecutionContext 

/**
 * A repository for people.
 *
 * @param dbConfigProvider The Play db config provider. Play will inject this for you.
 */
@Singleton
class CarroRepository @Inject() (dbConfigProvider: DatabaseConfigProvider)(implicit ec: ExecutionContext) 
  // We want the JdbcProfile for this provider
  private val dbConfig = dbConfigProvider.get[JdbcProfile]

  // These imports are important, the first one brings db into scope, which will let you do the actual db operations.
  // The second one brings the Slick DSL into scope, which lets you define the table and other queries.
  import dbConfig._
  import driver.api._

  /**
   * Here we define the table. It will have a name of people
   */
  private class CarroTable(tag: Tag) extends Table[Carro](tag, "carro") 

    /** The ID column, which is the primary key, and auto incremented */
    def id = column[Long]("id", O.PrimaryKey, O.AutoInc)

    /** The name column */
    def name = column[String]("name")

    /** The description column */
    def description = column[String]("description")

    /** The img column */
    def img = column[String]("img")

    /** The keywords column */
    def keywords = column[String]("keywords")

    /**
     * This is the tables default "projection".
     *
     * It defines how the columns are converted to and from the Person object.
     *
     * In this case, we are simply passing the id, name and page parameters to the Person case classes
     * apply and unapply methods.
     */
    def * = (id, name, description, img, keywords) <> ((Carro.apply _).tupled, Carro.unapply)
  

  /**
   * The starting point for all queries on the people table.
   */
  private val carro = TableQuery[CarroTable]

  /**
   * Create a person with the given name and age.
   *
   * This is an asynchronous operation, it will return a future of the created person, which can be used to obtain the
   * id for that person.
   */
  def create(name: String, description: String, img:String, keywords: String): Future[Carro] = db.run 
    // We create a projection of just the name and age columns, since we're not inserting a value for the id column
    (carro.map(p => (p.name, p.description, p.img, p.keywords))
      // Now define it to return the id, because we want to know what id was generated for the person
      returning carro.map(_.id)
      // And we define a transformation for the returned value, which combines our original parameters with the
      // returned id
      into ((nameAge, id) => Carro(id, nameAge._1, nameAge._2, nameAge._3, nameAge._4))
    // And finally, insert the person into the database
    ) += (name, description, img, keywords)
  

  /**
   * List all the people in the database.
   */
  def list(): Future[Seq[Carro]] = db.run 
    carro.result
  


封装模型

import play.api.data.Form
import play.api.data.Forms._

import play.api.libs.json._

case class Carro(id: Long, name:String, description:String, img:String, keywords:String)

case class CarroFormData(name: String, description: String, img: String, keywords: String)

object CarroForm 

  val form = Form(
    mapping(
      "name" -> nonEmptyText,
      "description" -> nonEmptyText,
      "img" -> nonEmptyText,
      "keywords" -> nonEmptyText
    )(CarroFormData.apply)(CarroFormData.unapply)
  )


object Carros 

  var carros: Seq[Carro] = Seq()

  def add(carros: Carro): String = 
    carros = carros :+ carro.copy(id = carro.length) // manual id increment 
    "User successfully added"
  

  def delete(id: Long): Option[Int] = 
    val originalSize = carro.length
    carro = carro.filterNot(_.id == id)
    Some(originalSize - carro.length) // returning the number of deleted users
  

  def get(id: Long): Option[Carro] = carro.find(_.id == id)

  def listAll: Seq[Carro] = carro

  implicit val carroFormat = Json.format[Carro]


查看代码

@(car: Form[CarroFormData])(implicit messages: Messages)

@import helper._

@main(new Main("Car Dealers", "Compra e venda de carros", "logo.png", "carro, compra, venda")) 
    <div class="container">
        <h1>Hello</h1>
@form(routes.AdminCarro.add()) 
        @inputText(person("name"))
        @inputText(person("description"))
        @inputText(person("img"))
        @inputText(person("keywords"))
        )

        <div class="buttons">
            <input type="submit" value="Add Car"/>
        </div>
    
    </div>

【问题讨论】:

它看起来像这样 views.html.admin.index(errorForm, Seq.empty[Carro]) 只接受一个参数 (car: play.api.data.Form[models.CarroFormData]) 并且你传递了 2 已经尝试从通话中删除其中一个,但没有解决问题,我将视图代码放在编辑中。 那么当您使用:Ok(views.html.admin.index(errorForm) 时,错误是什么样的?当您尝试通过浏览器访问页面时,它通常会显示错误所在。 现在它通过了那个错误并显示了另一个......但我设法解决了这个问题。 【参考方案1】:

在您的控制器代码处:

def add = Action  implicit request =>
  CarroForm.form.bindFromRequest.fold(
    // if any error in submitted data
    errorForm => Ok(views.html.admin.index(errorForm, Seq.empty[Carro])),
    data => repo.create(carro.name, carro.description, carro.img, carro.keywords).map  _ =>
      // If successful, we simply redirect to the index page.
      Redirect(routes.application.index)
    
  )

errorForm,您使用两个参数调用索引视图:

Ok(views.html.admin.index(errorForm, Seq.empty[Carro]))

但你的观点只声明了一个论点:

@(car: Form[CarroFormData])(implicit messages: Messages)

只需从控制器中的调用中删除Seq.empty[Carro],一切都会按预期工作。如果您仍然遇到相同的错误,请查看是否有其他地方以相同的错误方式调用此视图(使用两个参数)或尝试在 sbt run 之前 sbt clean 您的项目。

【讨论】:

以上是关于方法应用的参数太多:(汽车:play.api.data.Form[models.CarroFormData])的主要内容,如果未能解决你的问题,请参考以下文章

汽车保养猫腻太多,名悦集团教你轻松养车省钱省力

基于等效电路模型(RC)的锂离子电池参数在线辨识

Matlab 类方法:参数太多

函数参数太多的一种简化方法

移动端VIN码识别应用范围

@Scheduled注解各参数详解