2016-12-29 47 views
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 
+0

你的_form.html.erb的div是否帶有'error_messages'的ID? –

+0

我更改了代碼,並將div錯誤消息放在窗體中,但我沒有明白,需要在format.js的更新中指定渲染器嗎? –

回答

1

複製代碼new.js.erb,然後再試一次

+0

沒有什麼Gautam,而這種情況發生,更新,new.js和edit.js只做渲染到表單 –