我正在使用带有Rails的JQuery mobile,但是当我想从表中删除特定记录时,它最终会删除表中的所有内容..
<% @item.each do |basket| %>
<li data-icon="minus" id = "clicker"><a href="#">
<img src=<%= basket.item.item_media.url%> />
<h3><%= basket.item.brand %> </h3>
<p><%= basket.item.short_description %> </p></a>
<a><%= link_to "Delete From Basket",{:controller => "items", :action => "delete_from_basket", :item_id => basket.item.id}%></a>
</li><% end %> </ul>
这是我的delete_from_basket方法。
def delete_from_basket
random = params[:item_id]
@item = ConsumerUsersActivebasket.where(:user_id => current_user.id,:item_id => random).first.destroy
end
答案 0 :(得分:1)
尝试代替
<a><%= link_to "Delete From Basket",{:controller => "items", :action => "delete_from_basket", :item_id => basket.item.id}%></a>
这个
<%=link_to "Delete From Basket", item_delete_from_basket_path(basket.item)%>
在控制器中尝试:
def delete_from_basket
@item = Item.find(params[:item_id])
if @item.user_id == current_user.id
@item.destroy
end
end