gpt4 book ai didi

grails - Grails选择不设置默认值

转载 作者:行者123 更新时间:2023-12-02 15:14:01 27 4
gpt4 key购买 nike

我有一个应用程序,用户可以在其中填写表格,并保存一些预设以便快速重新填充


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
}

查看
        <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>

尽管我可以证明 User guide中描述的值等于true,但从不将其作为初始选择的值。是什么赋予了?

最佳答案

在Grails邮件列表上由Tomas Lin回答:

Your life would be easier if you just stick with ids.

Set an optionKey to equal the id of your object in the tag.

value = '${ favorite.id }' should now work.

关于grails - Grails选择不设置默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7719111/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com