jeudi 23 juillet 2015

Rails email form sending NULL input to JS and Controller in certain scenario

I've got a tricky one here.

So my application has roles for users. For example a person can be a Physician or can be a Delegate, and so on. In order to have the role of Delegate, that account must link to a Supervisors email address.

Here's my problem, if a user is already a Delegate, then the email input form (for the Delegate's Supervisor) passes a NULL input; to the javascript and to the controller. If a person is any other role, then the email input form passes the email as it should.

Even more weird is that there are two places in the app that an admin can update a users role. This is functioning perfectly fine in one of the two places.

So, some code. Here's the view html div of the place in the app this is functioning perfectly fine...

  <div class="delegates" style="<%= display_delegates ? '' : 'display:none;' %>">
        <h4>
        <i>...the following people</i>
        <div class="input-prepend input-append pull-right">
          <%= text_field_tag "email_search", "", class:'input-medium' %>
          <%= render partial:"/shared/spinner", locals:{size:'small', id:'delegate_add_spinner'} %>
          <label class="control-label" for="add_delegate">email:</label>
          <button class="btn" type="button" id='add_delegate' data-url='<%= user_add_delegator_path(user_id:@user.id) %>' data-yourl='<%= user_add_delegator_path(user_id:@user.id) %>'><i class="icon-plus"></i> Add...</button>
        </div>
        </h4>
        <div id="address_cards">
          <%= render partial: '/shared/address_card_mini', collection:user_form.object.delegator_users %>
        </div>
      </div>

Here's the view html div of the place in the app this is not functioning correctly... I've included some surrounding html because I believe this could be a deeper role-model issue

        <% when :edit %>
      <a class="role" name="/roles/<%= role.id %>/edit"><%= role.title %></a>
    <% when :select %>

          <% unless current_user.is_admin? && role.title == "Delegate" %>
          <label class="checkbox">
          <%= check_box_tag "user[role_ids][]", role.id, @user.try(:roles).include?(role) %> <%= role.title %>
          </label>
          <% else %>
          <label class="checkbox">
          <%= check_box_tag "user[role_ids][]", role.id, @user.try(:roles).include?(role), "data-show-node-when-checked" => 'div#role_delegates' %> <%= role.title %>
          </label>
          <% display_delegates = true if @user.roles.include? Role.find_by_title "Delegate"  %>
          <%#= error_for_instance_attribute @user, "delegate" %>
          <div id='role_delegates' class="delegates" style="<%= display_delegates ? '' : 'display:none;' %>">
            <h4>
            <i>Supervisors:</i>
            <div class="input-prepend input-append pull-right">
              <%= text_field_tag "email_search", "", class:'input-medium' %>
              <%= render partial:"/shared/spinner", locals:{size:'small', id:'delegate_add_spinner'} %>
              <label class="control-label" for="add_delegate">email:</label>
              <button class="btn" type="button" id='add_delegate' data-url='<%= user_add_delegator_path(user_id:@user.id) %>' data-yourl='<%= user_add_delegator_path(user_id:@user.id) %>'><i class="icon-plus"></i> Add...</button>
            </div>
            </h4>
            <div id="address_cards">
              <%= render partial: '/shared/address_card_mini', collection:@user.delegator_users %>
            </div>
          </div>

          <% end %>

    <% else %>
      <%= role.title %>
    <% end %>

Here's the controller action that updates the model...

Remember, the input is being passed a NULL when a user is already checked as a Delegate, so email="" during this scenario...

  def add_delegator
    email = params[:email].downcase
    debugger
    @delegator = Identity.where('LOWER(email) = ?', email).first().try(:user)
    @user = User.find params[:user_id]
    if @user.identity.email.downcase == email
      respond_to do |format|
        format.html  { head :unprocessable_entity }
      end
   else
    respond_to do |format|
      if @delegator
        if @user
          @user.delegator_users << @delegator unless @user.delegator_users.include? @delegator

          format.html  { render partial:'/shared/address_card_mini', object:@delegator }
        else
          format.html  { head :not_found }
        end
      else
        format.html  { head :not_found }
      end
    end
    end
  end

Here's the Javascript that performs an AJAX to confirm that the email is a correct email...

Remember, the input is being passed as NULL, so this is returning a 404 status when a user is already checked as a Delegate...

$(document).on('click', '#add_delegate', function() {
  RegistrationHelper.added_delegate = true;
  // var button = $(event.target);
  var button = $(this);
  var uri = button.data('url');
  if (typeof uri === 'undefined') {
    return false;
  }
  var input = $('#email_search');
  console.log(input);
  console.log(input.val());
  var data = {email:input.val().replace(/^\s+|\s+$/g, '')};
  var spinner = $('#delegate_add_spinner');

  spinner.css({display:'inline-block'});

  $.ajax({type:'POST', url: uri, data:data}).success(function(card) {

    var html = $(card);
    var data_id = html.attr('data-id');
    var existing_elem = $('.mini_addresscard[data-id=' + data_id + ']');
    if (existing_elem.length < 1) {

      input.popover('hide');
      // this is in a seperate timeout because IE8 is so damn stupid; it crashes if run directly
      setTimeout(function () {
        $('#address_cards').append(html);
        var last_card = $('#address_cards div.mini_addresscard').last();
        //last_card.get(0).innerHTML = card;
        // html.attr("id", 'sdklfjaklsdjf');
        last_card.css({display:'none'}).show('blind');
      }, 10);

    } else {

      var background = existing_elem.css('background');
      existing_elem.css({'background':'#FFFFAC'});
      existing_elem.animate({
        'background-color':'#EBEDF1'
      }, {
        complete: function() {
          existing_elem.css({background:background});
        }
      });
      // var border_color = existing_elem.css('border-color');
      // existing_elem.css({'border-color':'#EFF038'});
      // existing_elem.animate({'border-color':border_color});
    }

  }).complete(function(data) {
    spinner.hide();
  }).error(function(data) {
    var input = $('#email_search');
    input.popover("destroy");

    var error = 'Please try again later'; //incase something crazy happens
    if(data.status == "422"){
        error = 'You cannot add yourself as a supervisor.';
      }
    if(data.status == "404" ){
        error = 'Could not find anyone with that email address.';
      }
    add_popover(input, error);
    input.popover('show');
  });

  return false;
});

Aucun commentaire:

Enregistrer un commentaire