【问题标题】:undefined method `model_name' for 3:Fixnum3:Fixnum 的未定义方法“model_name”
【发布时间】:2015-09-17 21:22:44
【问题描述】:

我定义了一个辅助方法来预填充嵌套模型。

module MealsHelper
    def setup_meal( meal )
        add_recipes meal, 3
    end

    def add_recipes( meal,  number )
        number.times { meal.recipes.build }
    end
end

然后我将它添加到我的控制器中:

class MealsController < ApplicationController
  before_action :set_meal, only: [:show, :edit, :update, :destroy]
  helper MealsHelper

然后我从我的表单中调用它:

<%= form_for(setup_meal(@meal)) do |f| %>

现在我明白了:

undefined method `model_name' for 3:Fixnum

为什么 rails 会尝试为 Fixnum 寻找模型?

如果我跳过辅助方法,错误就会消失:

<%= form_for(@meal) do |f| %>

【问题讨论】:

    标签: ruby-on-rails ruby forms ruby-on-rails-4


    【解决方案1】:

    times 只会返回它正在迭代的 Fixnum。

    试试:

    def add_recipes( meal,  number )
        number.times { meal.recipes.build }
        return meal
    end
    

    【讨论】:

      猜你喜欢
      • 2012-02-26
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-30
      • 1970-01-01
      相关资源
      最近更新 更多