轨道 5:找不到具有友好 ID 的记录



我只是将friendly_id添加到我的应用程序中,除了一行代码出错之外,一切都很顺利。

当我尝试从我最喜欢的视图点葡萄酒时,我会出现以下错误。

can't find record with friendly id: "#<Wine::ActiveRecord_AssociationRelation:0x6133278>"

private
def set_wine
@wine = Wine.friendly.find(params[:id])
end

这是我对葡萄酒的看法:

<div class="col-md-3 right">
<%= link_to "Bestellen", wine_path(@wines), class: "btn btn-form" %>
</div>

找不到具有友好id的记录:葡萄酒::ActiveRecord_AssociationRelation:0x6133278

问题是@wines集合,而不是单个对象。因此,当您有wine_path(@wines)时,集合将作为id传递给控制器方法,而friendly_id期望单个记录。您必须更改

<%= link_to "Bestellen", wine_path(@wines), class: "btn btn-form" %>

<%= link_to "Bestellen", wine_path(favorite.wine), class: "btn btn-form" %>

以解决错误。

当您没有更新数据库中友好id之前的记录时,也可能会触发此错误

然后从rails控制台运行YouModel.find_each(&:save)

对我来说,发生错误是因为我使用了移动性。你应该添加到你的模型

friendly_id :slug_candidates, use: :mobility

而不是

friendly_id :slug_candidates, use: :slugged

最新更新