【问题标题】:RecordNotFound in Controller#editRecordNotFound 在控制器中#edit
【发布时间】:2018-09-05 02:17:58
【问题描述】:

我正在尝试在控制器中为嵌套资源创建编辑操作。当我尝试运行编辑操作时,我收到以下错误:

ActiveRecord::RecordNotFound in IngredientsController#edit
Couldn't find Recipe with 'id'=287

下面是我的控制器:

class IngredientsController < ApplicationController
  def edit
    @recipe = Recipe.find(params[:id])
    @ingredient = @recipe.ingredients.find(params[:id])
  end
end

还有我的编辑链接:

%= link_to 'Edit Ingredient', edit_recipe_ingredient_path(@recipe, ingredient) %>

任何想法可能导致这种情况?请让我知道是否需要任何其他代码来为问题提供上下文。谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby controller nested-resources


    【解决方案1】:

    尝试将edit 操作更改为:

    def edit
        @recipe = Recipe.find(params[:recipe_id])
        @ingredient = @recipe.ingredients.find(params[:id])
    end
    

    这似乎是与其他方法的唯一区别。另外,由于您在IngredientsController 中,所以params[:id] 是成分的id,而不是配方的。

    您似乎没有 Recipeid,这就是 Recipe.find(params[:id]) 抛出错误的原因。否则它不会失败,但成分最终会变成完全不同的配方。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-29
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多