Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AJAX提交后如何验证表单?_Javascript_Jquery_Ruby On Rails_Ajax_Jquery File Upload - Fatal编程技术网

Javascript AJAX提交后如何验证表单?

Javascript AJAX提交后如何验证表单?,javascript,jquery,ruby-on-rails,ajax,jquery-file-upload,Javascript,Jquery,Ruby On Rails,Ajax,Jquery File Upload,我有一个通过AJAX提交的表单,所以服务器的预期响应格式是javascript。问题是我不能让验证像这样工作。通常我会这样做: # POST /cats # POST /cats.json def create @cat = Cat.new(cat_params) respond_to do |format| if @cat.save format.html { redirect_to @cat, notice: 'Cat was suc

我有一个通过AJAX提交的表单,所以服务器的预期响应格式是javascript。问题是我不能让验证像这样工作。通常我会这样做:

  # POST /cats
  # POST /cats.json
  def create
    @cat = Cat.new(cat_params)

    respond_to do |format|
      if @cat.save
        format.html { redirect_to @cat, notice: 'Cat was successfully created.' }
        format.json { render action: 'show', status: :created, location: @cat }
      else
        format.html { render action: 'new' }
        format.json { render json: @cat.errors, status: :unprocessable_entity }
      end
    end
  end 
  # POST /cats
  # POST /cats.json
  def create
    @cat = Cat.new(cat_params)

    respond_to do |format|
      if @cat.save
        format.js { }
      else
        format.js { ????? }
      end
    end
  end
但现在我遇到了这样的情况:

  # POST /cats
  # POST /cats.json
  def create
    @cat = Cat.new(cat_params)

    respond_to do |format|
      if @cat.save
        format.html { redirect_to @cat, notice: 'Cat was successfully created.' }
        format.json { render action: 'show', status: :created, location: @cat }
      else
        format.html { render action: 'new' }
        format.json { render json: @cat.errors, status: :unprocessable_entity }
      end
    end
  end 
  # POST /cats
  # POST /cats.json
  def create
    @cat = Cat.new(cat_params)

    respond_to do |format|
      if @cat.save
        format.js { }
      else
        format.js { ????? }
      end
    end
  end
我已经创建了create.js.erb,如果保存了新对象,它可以完成我所需要的一切,但是如果没有保存对象,我不知道如何处理验证显示。在我的表单中,如果我不使用AJAX,它可以工作:

  <% if @cat.errors.any? %>
    <div id="error_explanation">
      <ul>
      <% @cat.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

我能做什么?我正在使用Rails 4。

您可以在成功和失败两种情况下呈现create.js.erb,并在create.js.erb文件中检查@cat.errors.any是否存在错误?如果是这样的话,您可以编写一些jquery并在html中插入错误,如果不是这样的话,您可以按照前面的成功场景处理

示例代码说明了我的意思,例如:

def create
  @cat = Cat.new(cat_params)

  respond_to do |format|
   @error = @cat.save
   // or you can use if if you need more in success case to be done before rendering
   format.js {}
  end
end
在create.js.erb中:

<% if @error %>
  // loop over errors like your working code in the case of html request
<% else %>
  // success scenario
<% end %>
在create.js.erb中,可以再次渲染表单,除非@cat.persisted

以另一部分中的表单为例,例如“\u form.html.erb”

如果您的表单位于id为my_form_div的div中

然后将创建create.js.erb


这将再次呈现整个表单以及错误。如果您不想再次呈现完整表单,则可以仅对errors div执行相同操作。

谢谢!你能告诉我一个如何在html中插入错误的小提示吗?好的,你知道如何使用jQuery吗不管最简单的方法是将上述代码的最后一个block-failure-case(问题中的最后一个block-failure-case)放在一个部分文件中,然后编写$id\u of\u error\u div.html