我一直在尝试用ajax来解决验证问题,因为我已经创建了一个部分,当事件发生时,调用部分错误,尽管,渲染,我不知道如何将它返回到模态中的表单视图,这里是我的代码:
_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 %>
create.js.erb
<% if @enterprise.errors.empty? %>
$('#exampleModal1').foundation('close');
<% else %>
$('#error_messages').html('<%= render partial: "enterprises/shared/errors" %>');
<% end %>
企业/共享/ _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 create
@enterprise = Enterprise.new(enterprise_params)
respond_to do |format|
if @enterprise.save
format.html { redirect_to @enterprise, notice: 'enterprise was successfully created.' }
format.js {}
format.json { render json: @enterprise, status: :created, location: @enterprise }
else
format.html { render action: "new" }
format.json { render json: @enterprise.errors, status: :unprocessable_entity }
end
end
end
答案 0 :(得分:0)
将create.js.erb的代码复制到new.js.erb并再试一次