Scala 为什么可以';我是否有新的方法作为我的用户控制器和路由在play框架中的一行中定义?

Scala 为什么可以';我是否有新的方法作为我的用户控制器和路由在play框架中的一行中定义?,scala,playframework,scala-2.10,playframework-2.3,Scala,Playframework,Scala 2.10,Playframework 2.3,我是scala和play框架的新手,如果这是一个幼稚的问题,请原谅。我一直在用Ruby on Rails和Rails的控制器构建我的web应用程序,我可以说: def new # my ruby code for rendering new template/view end 现在,我试着在游戏中做同样的事情(app/controllers/Users.scala): 你看到这个方法了吗 def new = Action { Ok("Hello new Users!!") }

我是scala和play框架的新手,如果这是一个幼稚的问题,请原谅。我一直在用Ruby on Rails和Rails的控制器构建我的web应用程序,我可以说:

def new
 # my ruby code for rendering new template/view
end
现在,我试着在游戏中做同样的事情(app/controllers/Users.scala):

你看到这个方法了吗

  def new = Action {
    Ok("Hello new Users!!")
  }
这就是play抛出此错误的地方:

Compilation error
identifier expected but 'new' found.
In ./first_app/app/controllers/Users.scala at line 16.
13    Ok("Hello Users!!")
14  }
15
16  def new = Action { 
17    Ok("Hello new Users!!")
18  }
19
20  def edit(id: Long) = Action {
21    Ok("Hello Users!!")
现在,我可以理解
new
是保留关键字还是什么,但对我来说没有意义。是的,如果我将其重命名为:
new\u user
,它可以工作,但这对我来说不是一个理想的情况(考虑到我可以在Rails中也这样做)

此外,我的路线:

# Home page
GET     /                           controllers.Application.index

GET     /users                      controllers.Users.index
GET     /users/new                  controllers.Users.new
GET     /users/:id                  controllers.Users.show(id: Long)
GET     /users/:id/edit             controllers.Users.edit(id: Long)
POST    /users                      controllers.Users.create
PUT     /users/:id                  controllers.Users.update(id: Long)
DELETE  /users/:id                  controllers.Users.destroy(id: Long)
有谁能告诉我是否可以在一行中定义所有
/users
相关路由


在Rails中,我可以做:
resources:users
,它过去负责
/users
的所有CRUD路由,Play框架中是否有类似的东西?在文档中似乎找不到它们。

new
在Scala和Java中,是一个不能用作名称的保留字,它用于创建某个类的新对象(实例),在Ruby中,如果我没有弄错的话,它是类上的一个方法,如
SomeClass.new
。你可以绕过它,改用
New
(带大写字母)。

哦,我想是的。这就是为什么我把它放在问题里。那么路由呢?@Surya我不使用Play,但我认为是的,路由只是一个映射到某个处理函数上的文本,因此不能将其用作处理程序名称,但作为路由的名称应该可以。在Ruby中,
new
是用于实例化类对象的类方法。如果我没有弄错的话,Java/Scala的情况也是如此?@Surya不,在Scala和Java中,它是一个,而不是一个方法。请注意,您可以引用标识符,否则使用反勾号将是非法的。提示:第一个操作使用
add
,这样可以避免与保留字冲突,语言不同,即使在现实生活中也是正常的;)@比西尔:谢谢你的提示。路线呢?可以在一行中定义所有CRUD路由吗?简短地说:不,不可能。我不确定我是否会使用add。这太像创造了。“换一个新的怎么样,它的含义与new大致相同。@calasyr:这就是为什么@4lex1v建议将其改为
new
,而不是
new
# Home page
GET     /                           controllers.Application.index

GET     /users                      controllers.Users.index
GET     /users/new                  controllers.Users.new
GET     /users/:id                  controllers.Users.show(id: Long)
GET     /users/:id/edit             controllers.Users.edit(id: Long)
POST    /users                      controllers.Users.create
PUT     /users/:id                  controllers.Users.update(id: Long)
DELETE  /users/:id                  controllers.Users.destroy(id: Long)