0
我一直在試圖解決ajax的驗證問題,因爲我創建了一個局部部分,當事件發生時,調用部分錯誤,雖然,渲染,我不'知道如何給它一個模式內返回到表單視圖,在這裏我的代碼:使用ajax Rails進行驗證的問題
_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 create.js.erb的
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
你的_form.html.erb的div是否帶有'error_messages'的ID? –
我更改了代碼,並將div錯誤消息放在窗體中,但我沒有明白,需要在format.js的更新中指定渲染器嗎? –