加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 02:53:38 所属栏目:百科 来源:网络整理
导读:我有这个表格 div id="login" h4Please 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 us
我有这个表格

<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>

所以我将数据远程设置为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));
});

然后这假设你在控制器响应中有这样的错误:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读