ruby-on-rails Rails 5:找不到具有友好ID的记录

2cmtqfgy  于 2023-01-06  发布在  Ruby
关注(0)|答案(3)|浏览(126)

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

ds97pgxw1#

找不到友好ID为的记录:Wine::活动记录_关联关系: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" %>

以解决错误。

pdkcd3nj

pdkcd3nj2#

如果尚未更新数据库中友好ID之前的记录,也可能会触发此错误
然后从rails控制台运行YouModel.find_each(&:save)

flvlnr44

flvlnr443#

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

friendly_id :slug_candidates, use: :mobility

代替

friendly_id :slug_candidates, use: :slugged

相关问题