如何返回未经验证的 AJAX/JSON 表单以进行更正?

发布于 2024-12-08 22:52:19 字数 1140 浏览 1 评论 0原文

我的网站的用户填写 AJAX/JSON 表单。

分解

  1. 我将 json 对象传递到外太空。
  2. 比对象在我的控制器中失败,
  3. 然后我的控制器加载第二个部分。
  4. 然后,当我单击并出现免责声明时,
  5. 第二部分消失。

我将 json 对象传递到外层空间。

            $.ajax({
                url: foo_url,
                data: dataString,
                type: "POST",
                dataType: 'json',
                success: function(data) {

该对象在我的控制器中失败

  respond_to do |wants|
    wants.json { render :json => {:errors => @card_signup.errors, :html => (render_to_string :partial => '/card_signups/new_form') } }
  end

然后我的控制器加载第二个部分。

这有效地隐藏了第一个部分,并将较新的放在上面。不知道这种情况在代码中如何或在何处发生,它只是因为控制器调用它而发生,我假设。

然后,当我单击并出现免责声明时

当免责声明出现时,最新的表单完全消失,只留下旧的未经验证的蹩脚表单。同样,系统中没有代码说明要执行此操作,这只是应用程序在加载两个表单并失败时执行的操作。

按发送

哎呀,它尝试注册唯一的表单(失败的表单),整个应用程序失败了。

大问题:

我怎样才能至少阻止对象渲染两次,并使其更新表单。这应该可以解决整个问题。

基本上,我不告诉它再次渲染 new_form,而是如何告诉它只返回包含错误的旧表单?

Users to my site, fill out an AJAX/JSON form.

The BreakDown

  1. i pass the json object to outer space.
  2. than the object fails in my controller,
  3. then my controller loads a second partial.
  4. then when i click and the disclaimer appears,
  5. second partial disappears.

i pass the json object to outer space.

            $.ajax({
                url: foo_url,
                data: dataString,
                type: "POST",
                dataType: 'json',
                success: function(data) {

than the object fails in my controller

  respond_to do |wants|
    wants.json { render :json => {:errors => @card_signup.errors, :html => (render_to_string :partial => '/card_signups/new_form') } }
  end

then my controller loads a second partial.

This effectively hides the first, and puts the newer one on top. No idea how or where this happens in the code, it just happens because the controller calls it I assume.

then when i click and the disclaimer appears

When the disclaimer appears, the newest form completely disappears just leaving the old unvalidated crappy one. Again, no code in the system says to do this, it's just what the application does when it loads two forms, and fails.

press send

Blam, it tries to register the only form ( the failed form ), and the whole application fails.

THE BIG QUESTION:

How can I at the very least just stop the object from rendering twice, and just make it so that it updates the form. That should pretty much resolve this entire issue.

Bascially instead of telling it to render the new_form again, how do I tell it to just return the old form with the errors in it?

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

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

发布评论

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

评论(2

情域 2024-12-15 22:52:29

查看用于发布表单并处理返回的 JSON 数据的 JavaScript 代码将有所帮助。

我有点同意 Vlad 的观点,即您不会返回表单的整个 HTML,而是编写一些 JavaScript 代码来在现有表单上显示错误消息。这实际上就是我处理它的方式,但如果您想将 JavaScript 保持在最低限度,您可以执行类似于以下操作的操作:

在您的页面上,这应该来自您的 new_form 部分:

<form id="my_form">
  ....
</form>

您的 javascript 提交表单并处理回调:

    $.ajax({
      url: foo_url,
      data: dataString,
      type: "POST",
      dataType: 'json',
      success: function(data) {
            if (data.errors) {
              $("#my_form").html(data.html); // replace old form
            }
            else {
              // Everything was OK, so do something here.  Perhaps reset the form?
            }
        }
    });

It would help to see your JavaScript code that is Posting the form and handling the JSON data that is returned.

I somewhat agree with Vlad that you do not return the entire HTML for the form, but rather write some JavaScript code to show error messages on the existing form. That is actually how I would handle it, but if you want to keep the JavaScript to a minimum you can do something similar to the following:

On your page, this should come from your new_form partial:

<form id="my_form">
  ....
</form>

Your javascript to submit the form and handle the callbacks:

    $.ajax({
      url: foo_url,
      data: dataString,
      type: "POST",
      dataType: 'json',
      success: function(data) {
            if (data.errors) {
              $("#my_form").html(data.html); // replace old form
            }
            else {
              // Everything was OK, so do something here.  Perhaps reset the form?
            }
        }
    });
晌融 2024-12-15 22:52:29

如果没有更多代码,我无法真正弄清楚为什么您的表单消失,但关于我的评论,它是这样的。
在 ajax 表单提交时,将数据发送到服务器进行验证。如果数据有效,则向客户端发送带有状态属性的 json 对象。如果数据无效,请向客户端发送相同的 json 对象,其中包含状态属性和另一个将保存错误消息的属性。它应该看起来像这样:

// validation ok
response = {status: 'ok'}

// validation errors
response = {status: 'error', msg: 'Invalid username<br />Invalid email addres...'}

在客户端检查响应的状态属性。如果等于“确定”,请执行某些操作,否则将response.msg 附加到页面上的任何容器元素,

我希望我说得清楚。
祝你好运

without some more code, i can't really figure out why your forms dissapear, but regarding my comment here it goes.
On ajax form submission send the data to ther server for validation. If data is valid send the client a json object with a status property. If data is invalid send the client the same json object with the status property and another property that will hold the error messages. It should look like this:

// validation ok
response = {status: 'ok'}

// validation errors
response = {status: 'error', msg: 'Invalid username<br />Invalid email addres...'}

On the client side check the status propery of response. If equals to 'ok' do something, else append the response.msg to any container element on your page

I hope i made myself clear.
Good luck

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