Grails 选择不设置默认值
我有一个应用程序,用户可以在其中填写表单,并保存一些预设以便快速重新填充
域
class Person {
String name
TeenageMutantNinjaTurtle favorite
static constraints = {
name blank:false, unique:true
favorite nullable:true
}
@Override
public String toString() { name }
}
package tmnt
class TeenageMutantNinjaTurtle {
String name
String colorHeadband
static constraints = {
name inList:["Leonardo", "Donatello", "Raphael", "Michelangelo"]
colorHeadband inList:["blue", "purple", "red", "orange" ]
}
@Override
public String toString() { "${name}" }
}
控制器
class PersonController {
def choose = {
if(session.user) {
def person = Person.findByName(session.user.username)
[
teenageMutantNinjaTurtleInstanceList: TeenageMutantNinjaTurtle.list(),
person : person,
favorite : person.favorite
]
}
}
def pickFavoriteTurtle = { TurtleCommandObject tut ->
def turtle = tut.turtleName
def choice = TeenageMutantNinjaTurtle.findByName(turtle)
String message = "Turtle not chosen "
if(choice){
def person = Person.findByName(tut.personName)
person.favorite = choice
person.save()
message = "Made ${person}'s favorite turtle ${choice}"
}
else {
message += "could not find ${choice}"
}
render message
}
查看
<div>
<h1>Hello ${person} </h1>
<g:form action="pickFavoriteTurtle">
<g:hiddenField name="personName" value="${person}" />
<g:select name="turtleName" from="${teenageMutantNinjaTurtleInstanceList}" value="${favorite}" />
<g:submitToRemote name="pickFavoriteTurtle"
url="[action:'pickFavoriteTurtle']"
value="Save your favorite turtle" />
</g:form>
</div>
最喜欢的值永远不会成为最初选择的值,尽管我可以证明它的计算结果等于 true,如 用户指南。什么给?
I've got an app where users can fill out a form, and save some pre-sets for quick re-population
Domain
class Person {
String name
TeenageMutantNinjaTurtle favorite
static constraints = {
name blank:false, unique:true
favorite nullable:true
}
@Override
public String toString() { name }
}
package tmnt
class TeenageMutantNinjaTurtle {
String name
String colorHeadband
static constraints = {
name inList:["Leonardo", "Donatello", "Raphael", "Michelangelo"]
colorHeadband inList:["blue", "purple", "red", "orange" ]
}
@Override
public String toString() { "${name}" }
}
Controller
class PersonController {
def choose = {
if(session.user) {
def person = Person.findByName(session.user.username)
[
teenageMutantNinjaTurtleInstanceList: TeenageMutantNinjaTurtle.list(),
person : person,
favorite : person.favorite
]
}
}
def pickFavoriteTurtle = { TurtleCommandObject tut ->
def turtle = tut.turtleName
def choice = TeenageMutantNinjaTurtle.findByName(turtle)
String message = "Turtle not chosen "
if(choice){
def person = Person.findByName(tut.personName)
person.favorite = choice
person.save()
message = "Made ${person}'s favorite turtle ${choice}"
}
else {
message += "could not find ${choice}"
}
render message
}
View
<div>
<h1>Hello ${person} </h1>
<g:form action="pickFavoriteTurtle">
<g:hiddenField name="personName" value="${person}" />
<g:select name="turtleName" from="${teenageMutantNinjaTurtleInstanceList}" value="${favorite}" />
<g:submitToRemote name="pickFavoriteTurtle"
url="[action:'pickFavoriteTurtle']"
value="Save your favorite turtle" />
</g:form>
</div>
The favorite is never made the initially selected value, even though I can show that it evaluates equals to true as described in the User guide. What gives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Tomas Lin 在 Grails 邮件列表上的回答:
Answered by Tomas Lin on the Grails mailing list:
有几件事。
value="${favorite.id}"
A couple of things.
value="${favorite.id}"