我与Resources&amp ;;有多态关联我的rails app中的标志。每个资源都可以由用户标记。
我正在尝试通过其编辑页面删除资源标志。这样,用户可以编辑资源,并通过ajax消除/销毁其Flags。
在数据库中一切正常,但是当我尝试使用ajax删除Flag时,除非我刷新页面,否则它不起作用。我正在使用此tutorial。
这是我的代码......
模型
class Resource
belongs_to :district
has_many :flags, as: :flaggable, :dependent => :destroy
end
class Flag
belongs_to :flaggable, polymorphic: true
end
控制器
Resource Controller
#edit page
def edit
@district = District.find_by_abbreviation(params[:district_id])
@resource = @district.resources.find(params[:id])
@flaggable = @resource
@flags = @resource.flags.all
end
end
Flags Controller
def destroy
@flag = Flag.find(params[:id])
@flaggable = @flag.flaggable
if @flag.destroy
respond_to do |format|
format.html { redirect_to @flaggable }
format.js
end
end
end
end
视图
edit.html.erb - 资源编辑页面
#renders all flags associated with this resource
<div id="flags">
<%= render @flags %>
</div>
<div>
#Code that holds edit form
</div>
_flag.html.erb
###delete action to destroy a flag
<div id="dom_id(flag)">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true"><%= link_to '×'.html_safe, [@flaggable, flag], method: :delete, data: { confirm: "Are you sure you want to delete this flag?" }, remote: true %></span>
</button>
<%= flag.message %>
</div>
destroy.js.erb
###I am not sure if this is correct...
$('#flags #<%= dom_id(@flag) %>').remove();
答案 0 :(得分:1)
我认为问题出在您的部分_flag.html.erb
应该是<div id="<%= dom_id(flag) %>">
而不是<div id="dom_id(flag)">