简单表单关联会出现“undefined method `klass' for nil:NilClass”错误

4
在我的Rails 3应用程序中,我有以下简单的关系结构:
class Rollout < ActiveRecord::Base
    has_many :items, :through => :rollout_items
end

class RolloutItem < ActiveRecord::Base
    belongs_to :rollout
    belongs_to :item
end

class Item < ActiveRecord::Base
    has_many :rollouts, :through => :rollout_items
end

控制器:


def new
    @rollout = Rollout.new
end

我遇到了以下表单所述的错误:

我用以下表单:

<%= simple_form_for @rollout do |f| %>
    <%= f.association :items %>
<% end %>

你的控制器中如何定义 @rollout - Sun
已更新以回答您的问题。 - danebez
RolloutRolloutItem之间的关系也已经定义好了,我想是这样的吧?即:has_many :rollout_items - Sun
没有,这就是缺失的部分。我假设在定义与项目的关系时它会被隐式定义。请发布您的解决方案,我会接受。 - danebez
1个回答

6

RolloutRolloutItem之间存在缺失的关联关系:

class Rollout < ActiveRecord::Base
    has_many :rollout_items # This.
    has_many :items, :through => :rollout_items
end

同样的道理也适用于 Item

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接