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