我只是将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