Grails 选择不设置默认值

发布于 2024-12-08 21:09:58 字数 2257 浏览 0 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鲜肉鲜肉永远不皱 2024-12-15 21:09:58

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

如果你坚持使用 ids,你的生活会更轻松。

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

value = '${ favorite.id }' 现在应该可以工作了。

Answered by Tomas Lin on the Grails mailing list:

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.

傾旎 2024-12-15 21:09:58

有几件事。

  1. 如果 favorite 是一个带有 id 的对象,你需要
    value="${favorite.id}"
  2. 您可以使用 value="${person.favorite.id}"

A couple of things.

  1. If favorite is an object with an id, you'll need
    value="${favorite.id}"
  2. You could just use value="${person.favorite.id}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文