Rails Ajax Requests



任何人都可以帮助解决这个问题吗? 我正在尝试使用 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中的嵌套资源,以防万一。 谢谢!

创建.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

新.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 %>

显示.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 %>

您正在尝试这样做:

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

因此,render(@client_sub_folder)需要client_sub_folders/_client_sub_folder中命名的部分_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) %>");

也可以工作并且不会引发异常。

更多详细信息:部分渲染器

希望这有帮助。

最新更新