Grails选择不设置默认值

时间:2011-10-10 21:49:14

标签: grails gsp

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

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>

最喜欢的东西永远不会成为最初选择的值,即使我可以证明它按照User guide中的描述评估等于真。是什么赋予了?

2 个答案:

答案 0 :(得分:2)

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

  

如果你坚持使用id,你的生活会更容易。

     

将optionKey设置为等于标记中对象的id。

     

value ='$ {favorite.id}'现在应该可以使用。

答案 1 :(得分:0)

有几件事。

  1. 如果收藏夹是带有ID的对象,则需要 值= “$ {favorite.id}”
  2. 你可以使用value =“$ {person.favorite.id}”