2017-07-25 136 views
0

任何人都可以協助解決這個問題。我試圖用Rails做一個AJAX請求,但不斷收到這個錯誤信息。Rails Ajax請求

ActionView::Template::Error (Missing partial client_sub_folders/_client_sub_folder with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :arb, :coffee, :jbuilder]}.

請在下面看到的代碼。我只是試圖添加到show頁面上的項目列表,同時保持同一頁面不刷新頁面。 client_sub_foldersclient_folders內的嵌套資源,以防有所作爲。謝謝!

create.js.erb

$("#new_client_sub_folder").remove(); 
$("#new_collection").show(); 
$("#item_list").append("<%= j render(@client_sub_folder) %>"); 

client_sub_folder_controller.rb

def create 
    @client_sub_folder = @client_folder.client_sub_folders.new(client_sub_folder_params) 

    respond_to do |format| 
     if @client_sub_folder.save 
     format.html { redirect_to client_folder_client_sub_folder_path(@client_folder, @client_sub_folder), notice: 'Client sub folder was successfully created.' } 
     format.json { render :show, status: :created, location: client_folder_client_sub_folder_path(@client_folder, @client_sub_folder) } 
     format.js {} 
     else 
     format.html { render :new } 
     format.json { render json: @client_sub_folder.errors, status: :unprocessable_entity } 
     format.js {} 
     end 
    end 
    end 

new.js.erb

$("#new_collection").hide().after("<%= j render('form') %>"); 

_form.html.erb (for client_sub_folders) 

    <%= form_for([@client_folder, @client_sub_folder], remote: true) do |f| %> 
     <% if @client_sub_folder.errors.any? %> 
     <div id="error_explanation"> 
      <h2><%= pluralize(@client_sub_folder.errors.count, "error") %> prohibited this @client_sub_folder from being saved:</h2> 

      <ul> 
      <% @client_sub_folder.errors.full_messages.each do |message| %> 
      <li><%= message %></li> 
      <% end %> 
      </ul> 
     </div> 
     <% end %> 
     <%= f.label :room_name %> 
     <%= f.text_field :room_name, required: true %> 

     <div class="actions"> 
     <%= f.submit %> 
     </div> 
    <% end %> 

show.html.erb(client_folders)

<div class="container"> 
    <div class="row"> 
     <p id="notice"><%= notice %></p> 
     <h1> <%= @client_folder.client_name %> Collection </h1> 

      <table> 
       <thead> 
       <tr> 
        <th colspan="3">Col A</th> 
        <th colspan="3">Col B</th> 
        <th colspan="3"> Col C </th> 
        <th colspan="3"> Col D </th> 
       </tr> 
       </thead> 

          <tbody> 

       <%= render partial: "client_sub_folders/client_sub_folder" %> 

      </tbody> 
      </table> 
      <%= link_to 'Create Client Collection', new_client_folder_client_sub_folder_path(@client_folder), class: "btn btn-success", id: "new_collection", remote: true %><br> 


     <%= link_to 'Back to Client Folders', designer_client_folders_path(current_designer) %> 
    </div> 
</div> 

_client_sub_folder.html.erb

<% @client_folder.client_sub_folders.each do |sub| %> 
<tr id="item_list"> 
        <td colspan="3"> <%= sub.room_name %> <%= link_to "(edit)", edit_client_folder_client_sub_folder_path(@client_folder, sub) %> </td> 
        <td colspan="3"> <%= sub.images.count unless sub.images.nil? %> </td> 
        <td colspan="3"><%= link_to 'Add/Remove', client_folder_client_sub_folder_path(@client_folder, sub) %></td> 
        <td colspan="3"><%= link_to 'Delete Collection', client_folder_client_sub_folder_path(@client_folder, sub), method: :delete, data: { confirm: 'Are you sure?' } %></td> 
        </tr><% end %> 
+0

可能是這行有問題'format.json {render json:@ client_sub_folder.errors,status :: unprocessable_entity}'您將請求作爲json傳遞? – Vishal

回答

0

您正在試圖做到這一點:

$("#item_list").append("<%= j render(@client_sub_folder) %>"); 

所以render(@client_sub_folder)需要的部分名爲_client_sub_folderclient_sub_folders/_client_sub_folder,以便它可以直接顯示細節。

在部分_client_sub_folder你可以把這樣的代碼:

<tr> 
    <td colspan="3"> <%= client_sub_folder.room_name %> <%= link_to "(edit)", edit_client_folder_client_sub_folder_path(client_sub_folder.client_folder, client_sub_folder) %> </td> 
    <td colspan="3"> <%= client_sub_folder.images.count unless sub.images.nil? %> </td> 
    <td colspan="3"><%= link_to 'Add/Remove', client_folder_client_sub_folder_path(client_sub_folder.client_folder, client_sub_folder) %></td> 
    <td colspan="3"><%= link_to 'Delete Collection', client_folder_client_sub_folder_path(client_sub_folder), method: :delete, data: { confirm: 'Are you sure?' } %></td> 
</tr> 

和創造的部分後,還可以改變show.html.erb你在哪裏做這個(在客戶端的子文件夾循環和顯示細節):

<tbody id="item_list"> 
    <% @client_folder.client_sub_folders.each do |sub| %> 
    <tr> 
     <td colspan="3"> <%= sub.room_name %> <%= link_to "(edit)", edit_client_folder_client_sub_folder_path(@client_folder, sub) %> </td> 
     <td colspan="3"> <%= sub.images.count unless sub.images.nil? %> </td> 
     <td colspan="3"><%= link_to 'Add/Remove', client_folder_client_sub_folder_path(@client_folder, sub) %></td> 
     <td colspan="3"><%= link_to 'Delete Collection', client_folder_client_sub_folder_path(sub), method: :delete, data: { confirm: 'Are you sure?' } %></td> 
    </tr> 
    <% end %> 
</tbody> 

你可以改變上面做這樣的:

<tbody id="item_list"> 
    <%= render partial: "client_sub_folder", collection: @client_folder.client_sub_folders %> 
</tbody> 

這你的代碼行後:

$("#item_list").append("<%= j render(@client_sub_folder) %>"); 

也將工作,而不是拋出異常。

更多詳細信息:PartialRenderer

希望這有助於。

+0

非常感謝這個@deep - 現在錯誤消失了,但現在唯一的問題是提交的對象,但我仍然需要刷新頁面以顯示新對象。對我現在失蹤的事情有任何想法? – dgreen22

+0

如果響應正確並呈現create.js.erb,請檢查您的響應是否正確,然後檢查它是如何響應的,如果沒有查看新代碼很難說。 – Deep