Problems with validations using Ajax in Rails

0

I'm trying to show the validation errors in Rails using ajax in a modal, but I can not show them, I hope they can help me, I will greatly appreciate, I share my code:

_form.html.erb

<%= form_for @enterprise, remote: true, html: { multipart: true } do |f| %>

  <div id="error-messages"></div>    

  <div class="row">
    <div class="small-6 columns">
      <%= f.label :nombre %>
      <%= f.text_field :name, placeholder: "Nombre" %>
    </div>
    <div class="small-6 columns">
      <%= f.label :correo_electronico %>
      <%= f.text_field :email, placeholder: "Correo electronico" %>
    </div>
  </div>

  <div class="row">
    <div class="small-6 columns">
      <%= f.label :contraseña %>
      <%= f.text_field :password, placeholder: "Contraseña" %>
    </div>
    <div class="small-6 columns">
      <%= f.label :confirmar_contraseña %>
      <%= f.text_field :password_confirmation, placeholder: "Confirmar contraseña" %>
    </div>
  </div>

  <div class="row">
    <div class="small-12 columns">
      <%= f.submit "Guardar", class: "button" %>
    </div>
  </div>

<% end %>

update.js.erb

<% if @enterprise.errors.empty? %>
  $('#exampleModal1').foundation('close');
<% else %>
  $('#error_messages').html('<%= render partial: "enterprises/shared/errors" %>');
<% end %>

enterprises / shared / _errors.html.erb

<% if @enterprise.errors.any? %>
  <div id="error_explanation">
    <h2>
      <%= pluralize(@enterprise.errors.count, "error") %> prohibited
      this article from being saved:
    </h2>
    <ul>
      <% @enterprise.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
<% end %>

enterprises_controller.rb

def update

    respond_to do |format|
      if @enterprise.update(enterprise_params)
        format.html { redirect_to admin_dashboard_path, notice: 'enterprise was successfully created.' }
        format.js   {}
        format.json { render json: @enterprise, status: :created, location: @enterprise }
      else
        format.html { render action: "edit" }
        format.js   { render action: "edit" }
        format.json { render json: @enterprise.errors, status: :unprocessable_entity }
      end
    end
  end
    
asked by Hector Hernandez 29.12.2016 в 20:46
source

1 answer

1

Problem solved = D My problem basically consisted of the following, if you look at the code, I'm trying to render the errors, but when the form is loaded to create or update, it would go through update. js , but in case the call to the render by some form does not do it, so the only thing that I did since taking advantage in the controller I do a render "edit", inside format.js , eliminate the render and add again the error code in the form where it belongs initially and in this way shows them without any problem, in conclusion the only thing that is needed is to leave the error code in the form or make a simple render without js, and within the controller inside update and create, add the format.js { render action: "edit" } and it goes great = D

    
answered by 31.12.2016 в 06:50