ruby-on-rails - Rails 3 - 当 Data-Remote 为 True 时显示错误

标签 ruby-on-rails ajax

我有这个表格

<div id="login">

  <h4>Please log in with your email address</h4>
  <%= form_for(@user, id: "login", remote: true) do |f| %>
    <% if @user.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

        <ul>
        <% @user.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
        </ul>
      </div>
    <% end %>

    <div class="field">
      <%= f.label :name %><br />
      <%= f.text_field :name %>
    </div>
    <div class="field">
      <%= f.label :email %><br />
      <%= f.text_field :email %>
    </div>
    <div class="field">
      <%= f.label :company %><br />
      <%= f.text_field :company %>
    </div>
    <div class="field">
      <%= f.label :phone %><br />
      <%= f.text_field :phone %>
    </div>
    <div class="field">
      <%= f.label :address %><br />
      <%= f.text_field :address %>
    </div>
    <div class="actions">
      <%= f.submit id: "signup_submit" %>
    </div>
  <% end %>
  <script>
      $("#signup_submit").bind("ajaxSend", function(){
           alert('start');
         }).bind("ajaxComplete", function(){
           alert('end');
         });
  </script>
</div>

所以我将 data-remote 设置为 true,并且我很难显示在它降级为 HTTP 请求时会起作用的错误。

我相信这很简单,希望得到任何帮助。

谢谢。

最佳答案

首先重组这个,使error_explanation div 存在于页面上(因此您可以使用错误消息填充它...如果需要,可以使用 CSS 隐藏它)

<div id="error_explanation">
  <% if @user.errors.any? %>
    <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

    <ul>
    <% @user.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  <% end %>
</div>

然后你可以将这段 jQuery 代码添加到你现有的 <script> 中。标签区:
// a method for parsing the JSON response
handle_ajax_error = function(response) {
  try { var responseText = jQuery.parseJSON(response.responseText); } catch(e) { var responseText = null; }
  if (responseText !== null) { 
    var responseMsg = responseText.error; 
  } else {
    responseMsg = 'There was an unknown error or the request timed out.  Please try again later';
  }

  return responseMsg;
}

// callback for the ajax error on the #login form
$('#login').live('ajax:error', function(data, xhr, response){ 
  $('#error_explanation').html(handle_ajax_error(response));
});

然后这假设您的 Controller 响应中有这样的错误:
def create
  @user = User.create(params[:user])
  if @user.save
    redirect_to user_path(@user)
  else
    render :json => { :error => @user.errors.full_messages.to_sentence }, 
           :status => :unprocessable_entity
  end
end

关于ruby-on-rails - Rails 3 - 当 Data-Remote 为 True 时显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8851721/

相关文章:

ruby-on-rails - Rails WHERE 查询 has_many 关系不正确?

javascript - 需要重新加载页面才能加载 JavaScript?

ruby-on-rails - Rails - 有两个 parent 的嵌套资源

javascript - 在 Ajax 中发送复选框值

ajax 中的 php ajax

javascript - 在 React 中使用 componentWillRecieveProps 隐藏组件与处理 props

ruby-on-rails - 用于 ruby​​/ruby on rails 的 SOAP 客户端生成器

javascript - rails + Javascript : How do I create a button that slides/changes a form from left to right?

javascript - 同一域上的 iframe 的权限被拒绝 - Apache vhost cfg 问题?

javascript - 记住 JavaScript 跨页幻灯片中的图像编号