繁体   English   中英

Rails远程表单未触发Ajax:成功处理程序

[英]Rails remote form not triggering ajax:success handler

这是我对表单的看法:

#popupBox
  %h1 Change Password
  %p Update password for user: #{@user.email}.
  #formHolder
    = simple_form_for @user, url: update_password_user_path, html: { class: 'remote-form' }, remote: true do |f|
      = f.input :password, required: true

      = f.button :submit, 'Update Password'

这是控制器代码:

def update_password
  @user = User.find(params[:id])
  if @user.update(update_password_params)
    render layout: false # This shows a nice "Completed" message for the user.
  else
    render action: 'edit_password', layout: false
  end
end

和我的JavaScript代码:

$(document).ready ->
  $("form.remote-form").on("ajax:success", (e, data, status, xhr) ->
    console.log("Working")
    $("form.remote-form").append xhr.responseText
  ).on "ajax:error", (e, xhr, status, error) ->
    console.log("Not working")
    $("form.remote-form").append "<p>ERROR</p>"

当我提交表单时,会触发AJAX帖子(在我的开发工具中可见),但不会触发console.log调用。

如何检测何时通过AJAX提交表单,并将控制器代码的结果放入dom中的表单位置?

在控制器中添加json响应:

  def update_password
    @user = User.find(params[:id])

    respond_to do |format|
      if @user.update(update_password_params)
        format.json { render json: @user, status: :updated }
      else
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM