Grails - 非常简单的集合集合值不起作用

发布于 2024-09-10 05:36:36 字数 983 浏览 8 评论 0原文

Bug 的控制器:

这是 bug 的创建方法,我打印了 bugInstance.activities ,其中包含我的活动对象

def create = {
        def bugInstance = new Bug()
        def activity = new Activity(description:"created")

        bugInstance.properties = params
        bugInstance.addToActivities(activity)
        return [bugInstance: bugInstance]
    }

然后我查看了 save 方法,并打印了完全相同的内容,结果为空,所以不知何故它丢失了我创建的活动,我不知道为什么。这真的是默认行为吗?我是否在做一些非常基本的错误,因为似乎没有任何理由这样一段简单的代码不起作用。

def save = {

    def bugInstance = new Bug(params)
    println bugInstance.activities
    if (bugInstance.save(flush: true)) {
        flash.message = "${message(code: 'default.created.message', args: [message(code: 'bug.label', default: 'Bug'), bugInstance.id])}"
        redirect(action: "show", id: bugInstance.id)
    }
    else {
        render(view: "create", model: [bugInstance: bugInstance])
    }
}

我知道我可以通过在 save 方法中添加活动来解决这个问题,但为什么我会从 create() -> 中丢失活动节省()

Controller for a Bug:

this is the create method for a bug, I printed out bugInstance.activities and it had my activity object in it

def create = {
        def bugInstance = new Bug()
        def activity = new Activity(description:"created")

        bugInstance.properties = params
        bugInstance.addToActivities(activity)
        return [bugInstance: bugInstance]
    }

Then I looked at the save method, and printed the exact same thing, and the result is null, so somehow it's lost the activity I created, and I have no idea why. Is this really the default behavior? Am I doing something really basic wrong, because there doesn't seem to be any reason such a simple piece of code wouldn't work.

def save = {

    def bugInstance = new Bug(params)
    println bugInstance.activities
    if (bugInstance.save(flush: true)) {
        flash.message = "${message(code: 'default.created.message', args: [message(code: 'bug.label', default: 'Bug'), bugInstance.id])}"
        redirect(action: "show", id: bugInstance.id)
    }
    else {
        render(view: "create", model: [bugInstance: bugInstance])
    }
}

I know I can work around this by adding the activity in the save method, but why do I lose the activity from create() -> save()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

随心而道 2024-09-17 05:36:37

可能听起来像一个愚蠢的问题,但是您是否在 create.gsp 中设置了隐藏参数或任何内容,并在创建中实例化了 Bug 实例?我的意思是,我不认为你在这里做的事情有什么问题。你的 create.gsp 是什么样的?

might sound like a stupid question but are you setting a hidden parameter or anything in the create.gsp with the Bug Instance instantiated in the create?? I mean, I don't see anything wrong with what your doing here. What does your create.gsp look like?

苦行僧 2024-09-17 05:36:37

如果您使用自动生成的 create.gsp,则活动集将不会包含在表单中。它当然存在于模型中,但客户端不会呈现活动字段。当回来保存时,很明显活动丢失了。根据您想要实现的目标,您可以将一些活动选择器添加到 create.gsp 或(首先)包含活动描述的隐藏字段,但然后在保存操作中,我想您必须处理任何活动中的活动参数在这种情况下,grails 的魔力并没有达到为您实例化 Activity 的程度。与在保存操作中实例化 Bug 本身的方式相同,如果您希望持久保存 Activity,则必须实例化 Activity,甚至保存它。

编辑:如果您确实想传递整个活动列表,则可以使用索引属性。

在 create.gsp 中添加以下内容:

<g:each status="i" var="activity" in="${bugInstance.activities}">
  <!-- one hidden field for each property of each attached activity  -->
  <g:hiddenField
    name="activities[${i}].description"
    value="${activity.description}" />
</g:each>

在 save 方法中添加以下内容:

params.activities.each{ activity ->
  bugInstance.addToActivities(new Activity(activity))
}

但在您的情况下,从单个字段实例化一个活动可能就足够了。

If you are using the auto-generated create.gsp the activities set will not be included in the form. It is of course there in the model, but there will be no activities field rendered on the client side. When it comes back to save, it is clear that the activity is lost. Depending on what you want to achieve you could add some activity selector to the create.gsp or (to start with) a hidden field with your activities description, but then in the save action I guess you have to handle the the activities param in any case, as the magic of grails doesn't go as far as instanciating the Activity for you. The same way as you instantiate the Bug itself in the save action, you have to instantiate the Activity and even save it, if you want it to be persisted.

Edit: If you really want to pass around the whole activities list, you could make use of indexed properties.

In create.gsp add this:

<g:each status="i" var="activity" in="${bugInstance.activities}">
  <!-- one hidden field for each property of each attached activity  -->
  <g:hiddenField
    name="activities[${i}].description"
    value="${activity.description}" />
</g:each>

And in the save method this:

params.activities.each{ activity ->
  bugInstance.addToActivities(new Activity(activity))
}

But in your case it might be sufficient to instantiate the one activity from a single field.

捎一片雪花 2024-09-17 05:36:36

您永远不会在新实例上调用 save() :

def create = {
   def bugInstance = new Bug()
   def activity = new Activity(description:"created")

   bugInstance.properties = params
   bugInstance.addToActivities(activity)
   bugInstance.save()
   return [bugInstance: bugInstance]
}

您不需要保存 Activity,因为它会被传递保存,因为它位于活动集合中。

You never call save() on the new instance:

def create = {
   def bugInstance = new Bug()
   def activity = new Activity(description:"created")

   bugInstance.properties = params
   bugInstance.addToActivities(activity)
   bugInstance.save()
   return [bugInstance: bugInstance]
}

You don't need to save the Activity because it'll be transitively saved since it's in the activities collection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文