Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 - Displaying errors when Data-Remote is True

I have this form

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

So I have data-remote set to true, and I am having a hard time displaying the errors that otherwise work when it degrates to HTTP requests.

I am sure this is simple and would appreciate any help.

Thanks.

like image 354
Ryan Haywood Avatar asked Feb 21 '23 20:02

Ryan Haywood


2 Answers

First reorganize this so that the error_explanation div exists on the page (so you can populate it with an error message... hide it with CSS if you need to)

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

Then you can add this bit of jQuery code to your existing <script> tag area:

// 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));
});

And then this assumes that you have something like this in your controller response for the error:

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
like image 97
iwasrobbed Avatar answered Mar 04 '23 20:03

iwasrobbed


There is a more simpler solution for this problem using this gem (https://github.com/bcardarella/client_side_validations). This gem is pretty much easy to install in Rails application and client side validation of form fields becomes very easy using this gem.

like image 32
K M Rakibul Islam Avatar answered Mar 04 '23 21:03

K M Rakibul Islam