[英]Rails - removing li after destroying containing object
I'm building a project app for a full stacking dev bootcamp program. 我正在为一个完整的堆叠开发训练营程序构建一个项目应用程序。 I'm implementing a destroy method which removes a given item from a list.
我正在实现一个destroy方法,它从列表中删除一个给定的项目。 I'm implementing an AJAX request which should remove the item's li from the list.
我正在实现一个AJAX请求,它应该从列表中删除项目的li。 The destroy method is working as should, meaning that when I reload the page the element is gone, but it isn't happening without reloading the page.
destroy方法正常工作,这意味着当我重新加载页面时元素已经消失,但是没有重新加载页面就不会发生。
My items are rendered via a partial: 我的项目通过部分呈现:
<ul class="list-group">
<% @list.items.each do |item| %>
<!-- <li id="item-<%= @item.id %>" class="list-group-item"> -->
<li class="list-group-item">
<%= item.name %>
<%= link_to "", [item.list, item], method: :delete, remote: true, class: 'glyphicon glyphicon-ok' %>
</li>
<% end %>
</ul>
My destroy.js.erb file: 我的destroy.js.erb文件:
<% if @item.destroyed? %>
$('#item-' +<%= @item.id %>).hide();
// $('#item-' +<%= @item.id %>).closest('li');
// self.closest('li').remove();
// $('li#item-<%= @item.id %>').hide();
// $('#item-' +<%= @item.id %>).parent().remove();
// $("li").remove('li#item-<%= @item.id %>');
<% else %>
$('#item-' +<%= @item.id %>).prepend("<div class='alert alert-danger'><%= flash[:error] %></div>");
<% end %>
In my items_controller.rb: 在我的items_controller.rb中:
def destroy
@list = List.find(params[:list_id])
@item = Item.find(params[:id])
authorize @item
if @item.destroy
flash[:notice] = "Task Completed."
# redirect_to @list
else
flash[:error] = "Task couldn't be deleted. Try again."
end
respond_to do |format|
format.html
format.js
end
end
Terminal output when selecting list item to be deleted: 选择要删除的列表项时的终端输出:
Started DELETE "/lists/28/items/105" for ::1 at 2015-04-13 16:51:57 -0400
Processing by ItemsController#destroy as JS
Parameters: {"list_id"=>"28", "id"=>"105"}
List Load (0.1ms) SELECT "lists".* FROM "lists" WHERE "lists"."id" = ? LIMIT 1 [["id", 28]]
Item Load (0.0ms) SELECT "items".* FROM "items" WHERE "items"."id" = ? LIMIT 1 [["id", 105]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 8]]
(0.1ms) begin transaction
SQL (0.2ms) DELETE FROM "items" WHERE "items"."id" = ? [["id", 105]]
(1.5ms) commit transaction
Rendered items/destroy.js.erb (0.6ms)
Completed 200 OK in 10ms (Views: 5.9ms | ActiveRecord: 2.0ms)
The commented out lines are various methods I have tried to get the element to remove. 注释掉的行是我尝试删除元素的各种方法。 Any help is greatly appreciated.
任何帮助是极大的赞赏。
When you output the li
you should attach dom_id(@item)
so you can reference it later 当您输出
li
您应该附加dom_id(@item)
以便稍后引用它
<li class="list-group-item" id="<%= dom_id(@item) %>">
Then you should be able to 然后你应该能够
$("#<%= dom_id(@item) %>").remove();
Also, this is kind of bad because you're not verifying that @item
belongs to a particular @list
此外,这有点不好,因为您没有验证
@item
属于特定的@list
@list = List.find(params[:list_id])
@item = Item.find(params[:id])
This is better 这个更好
@list = List.find(paramas[:list_id])
@item = @list.items.find(params[:id])
Before this change: 在此更改之前:
DELETE /lists/1/items/2
Would work even if Item#2 doesn't belong to List#1. 即使Item#2不属于List#1也能工作。
After this change: 在此更改后:
DELETE /lists/1/items/2
Will not work unless Item#2 belongs to List#1 除非项目#2属于列表#1,否则将无法工作
You could use these lines of html 你可以使用这些html行
<% @list.items.each do |item| %>
<li id="item-<%= item.id %>" class="list-group-item">
....
so you have a unique identifier for every item. 所以你有每个项目的唯一标识符。 not sure why you have commented it out.
不知道你为什么评论它。
Then do this in your destroy.js.erb 然后在你的destroy.js.erb中执行此操作
var item = $('li#item-' + <%= @item.id %>);
item.hide();
// or
item.remove();
Your controller and server response look fine. 您的控制器和服务器响应看起来很好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.