创建视图中的表单值不会传递到保存视图

发布于 2024-12-02 15:51:40 字数 2285 浏览 0 评论 0原文

这个问题是 / 的扩展,也与我问的问题类似 此处。我在该问题中遇到的具体问题已解决,但我遇到了另一个非常相似的问题。

问题是在 create 视图中输入的“Cargo Destination”和“Cargo Source”值不会显示在save 视图中。例如,当用户忘记在创建视图中输入必要的信息并按下“创建”按钮时,用户输入的值应显示在保存视图中> 查看。发生这种情况时,除“Cargo Destination”和“Cargo Scource”之外的所有字段都会显示在save 视图中。

当我将 println 语句放入 save 控制器中时,我可以看到相关参数确实从创建视图。

有问题的两个字段链接到其他字段,并由 AJAX 填充,该 AJAX 进入 Load 控制器中的闭包。我相信这个闭包是我的问题所在,在render语句中:

def getAccountUserCargoDestinations = {
    if(params.id == ""){
        render g.select(name: 'cargoDestination.id')
        return
    }
    def user = Account.find("from Account as account where account.id=:id", [id:Long.valueOf(params.id)]).user
    def addresses = Address.find("from Address as addresses where addresses.user=:user and addresses.cargoDestination=true", [user:user])
    render g.select(optionKey: 'id', from: addresses,  name: 'cargoDestination.id')
}

如果这不是问题所在,那么它可能在render语句中的相应字段中>create 视图本身:

<g:select name="cargoDestination.id" optionKey="id" value="${loadInstance?.cargoDestination?.id}" />

另外两件事:

1.) Grails 没有给我任何错误

2.) 一个 cargoDestination 和一个 cargoSource 都是以下实例我制作的 Address 类。

任何帮助将不胜感激。

更新:

在继续寻找解决方案时,我注意到了一些有趣的事情。首先,我将以下代码放入我的 create 视图中(该视图由 Load 控制器中的 create 闭包渲染,并由 save 重新渲染> 加载控制器中的闭包(如果用户将任何字段留空):

  <g:if test="${loadInstance?.cargoDestination?.id != null}">
    ${loadInstance?.cargoDestination}
  </g:if>

通过这样做,我可以看到当 调用该参数时,该参数一直到达 create 视图保存关闭。我想我可以使用 ;标签以获得所需的结果,但这看起来很混乱,我怀疑这是解决这个问题的“正确”方法。我还将我的 AJAX 包装在类似的标签中,以确保当save闭包呈现create视图时它不会更改字段值,并且我可以确认 AJAX 确实没有更改字段值。除了 AJAX 之外,我的问题字段的唯一不同之处是我在相应的` 标记中没有from属性(如我最初发布的代码所示)这个问题)。我知道解决方案很简单...我在这里缺少什么?

This question is an extension of / is similar too the question I asked here. The specific issue I was having in that question was solved, but I am having another very similar issue.

The issue is that the "Cargo Destination" and "Cargo Source" values entered in the create view do not show up in the save view. For instance, when a user forgets to enter a necessary bit of information in the create view and presses the "Create" button, the values the user entered should show up in the save view. All fields except the "Cargo Destination" and "Cargo Scource" show up in the save view when this scenario occurs.

When I put a println statement in my save controller, I can see that the parameters in question did indeed make it to the save controller from the create view.

The two fields in question are chained to other fields and populated by an AJAX that goes to a closure in the Load controller. I believe this closure is where my problem is, in the render statement:

def getAccountUserCargoDestinations = {
    if(params.id == ""){
        render g.select(name: 'cargoDestination.id')
        return
    }
    def user = Account.find("from Account as account where account.id=:id", [id:Long.valueOf(params.id)]).user
    def addresses = Address.find("from Address as addresses where addresses.user=:user and addresses.cargoDestination=true", [user:user])
    render g.select(optionKey: 'id', from: addresses,  name: 'cargoDestination.id')
}

If that is not where the problem is, then it is probably in the corresponding field of the create view itself:

<g:select name="cargoDestination.id" optionKey="id" value="${loadInstance?.cargoDestination?.id}" />

Two more things:

1.) Grails is not giving me any errors

2.) A cargoDestination and a cargoSource are both instances of the Address class which I made.

Any help would be appreciated.

UPDATE:

Continuing to look for a solution I noticed several interesting things. First I put the following code in my create view (which is rendered by the create closure in the Load controller, and re-rendered by the save closure in the Load controller IF the user leaves any fields blank):

  <g:if test="${loadInstance?.cargoDestination?.id != null}">
    ${loadInstance?.cargoDestination}
  </g:if>

By doing this I can see that the parameter is making it all the way to the create view when it is called by the save closure. I guess I could use <g:if> tags to get the desired results, but that just seems messy, and I doubt that is the "correct" way to solve this problem. I also wrapped my AJAX in a similartag to make sure it was not changing the field value when thesaveclosure rendered thecreateview, and I can confirm that the AJAX is indeed not changing the field value. Other than the AJAX, the only thing different about my problem fields is that I do not have afromattribute in their corresponding` tags (as can be seen by the code I posted originally in this question). I know the solution is something simple... What am I missing here?

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

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

发布评论

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

评论(2

痴梦一场 2024-12-09 15:51:40

有问题的两个字段链接到其他字段并填充
通过 AJAX 进入负载控制器中的闭包。

嗯。对象是否保存在中间?读起来就好像

  • 数据被发送到 save 控制器,该控制器不会保存,因为缺少字段,
  • 保存控制器显示 save 视图和 println 是字段(可供控制器使用),
  • 视图调用 load 控制器,该控制器尝试从尚未保存的对象中检索两个字段。没有与save控制器的中间请求数据的连接。

如果不是这个问题,我想您将不得不发布更多代码才能分析您的问题。

嗯。刚刚看了你的另一个问题。我认为 Ajax load 控制器没有机会获取数据,因为 save 控制器没有将数据保存到数据库中。

为什么不直接通过 save 视图输出这些值?

The two fields in question are chained to other fields and populated
by an AJAX that goes to a closure in the Load controller.

hm. Is the object saved in between? It reads as if

  • the data is sent to the save controller which does not save because there are missing fields
  • the save controller displays the save view and println's the field (which are available to the controller)
  • the view invokes the load controller which tries to retrieve two fields from an object which isn't saved yet. There is no connection to the intermediate request data of the save controller.

if it's not this problem, I guess you'll have to post more code in order to get your problem analyzed.

hm. just taken a look at your other question. I see no chance for the Ajax load controller to get the data since the save controller did not persist the data to the database.

Why don't you output these values directly through the save view?

貪欢 2024-12-09 15:51:40

终于通过排除法弄清楚了,但现在我看到了解决方案,它应该是非常明显的。

我所需要做的就是将 from 属性添加到受影响的 标记中,如下所示:

<g:select from="${loadInstance?.cargoDestination}" name="cargoDestination.id" optionKey="id" value="${loadInstance?.cargoDestination?.id}" />

现在完全按预期工作。

Finally figured it out through process of elimination, but now that I see the solution it should have been very evident.

All I needed to do was add the from attribute to the affected <g:select> tags, like so:

<g:select from="${loadInstance?.cargoDestination}" name="cargoDestination.id" optionKey="id" value="${loadInstance?.cargoDestination?.id}" />

Works exactly as expected now.

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