Ruby on rails 字段的复数形式\u for有\u多个关联未显示在视图中

Ruby on rails 字段的复数形式\u for有\u多个关联未显示在视图中,ruby-on-rails,ruby,ruby-on-rails-3,nested-attributes,fields-for,Ruby On Rails,Ruby,Ruby On Rails 3,Nested Attributes,Fields For,目前,一个项目属于一个公司,并且有许多项目变体 我尝试使用嵌套字段\u通过项目表单添加ItemVariant字段,但是使用:Item\u variants不会显示表单。它仅在我使用单数时显示 我已经检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项有关,还是我遗漏了其他内容 提前谢谢 注意:下面的代码片段中省略了不相关的代码 编辑:不知道这是否相关,但我正在使用CanCan进行身份验证 routes.rb resources :companies do resources :item

目前,一个项目属于一个公司,并且有许多项目变体

我尝试使用嵌套字段\u通过项目表单添加ItemVariant字段,但是使用:Item\u variants不会显示表单。它仅在我使用单数时显示

我已经检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项有关,还是我遗漏了其他内容

提前谢谢

注意:下面的代码片段中省略了不相关的代码

编辑:不知道这是否相关,但我正在使用CanCan进行身份验证

routes.rb

resources :companies do
  resources :items
end
class Item < ActiveRecord::Base
  attr_accessible :name, :sku, :item_type, :comments, :item_variants_attributes


  # Associations
  #-----------------------------------------------------------------------
  belongs_to :company
  belongs_to :item_type
  has_many   :item_variants

  accepts_nested_attributes_for :item_variants, allow_destroy: true

end
class ItemVariant < ActiveRecord::Base
  attr_accessible :item_id, :location_id

  # Associations
  #-----------------------------------------------------------------------
  belongs_to :item

end
<%= form_for [@company, @item] do |f| %>
  ...
  ...
  <%= f.fields_for :item_variants do |builder| %>
    <fieldset>
      <%= builder.label :location_id %>
      <%= builder.collection_select :location_id, @company.locations.order(:name), :id, :name, :include_blank => true %>
    </fieldset>
  <% end %>
  ...
  ...
<% end %>
item.rb

resources :companies do
  resources :items
end
class Item < ActiveRecord::Base
  attr_accessible :name, :sku, :item_type, :comments, :item_variants_attributes


  # Associations
  #-----------------------------------------------------------------------
  belongs_to :company
  belongs_to :item_type
  has_many   :item_variants

  accepts_nested_attributes_for :item_variants, allow_destroy: true

end
class ItemVariant < ActiveRecord::Base
  attr_accessible :item_id, :location_id

  # Associations
  #-----------------------------------------------------------------------
  belongs_to :item

end
<%= form_for [@company, @item] do |f| %>
  ...
  ...
  <%= f.fields_for :item_variants do |builder| %>
    <fieldset>
      <%= builder.label :location_id %>
      <%= builder.collection_select :location_id, @company.locations.order(:name), :id, :name, :include_blank => true %>
    </fieldset>
  <% end %>
  ...
  ...
<% end %>
class项
项目变量.rb

resources :companies do
  resources :items
end
class Item < ActiveRecord::Base
  attr_accessible :name, :sku, :item_type, :comments, :item_variants_attributes


  # Associations
  #-----------------------------------------------------------------------
  belongs_to :company
  belongs_to :item_type
  has_many   :item_variants

  accepts_nested_attributes_for :item_variants, allow_destroy: true

end
class ItemVariant < ActiveRecord::Base
  attr_accessible :item_id, :location_id

  # Associations
  #-----------------------------------------------------------------------
  belongs_to :item

end
<%= form_for [@company, @item] do |f| %>
  ...
  ...
  <%= f.fields_for :item_variants do |builder| %>
    <fieldset>
      <%= builder.label :location_id %>
      <%= builder.collection_select :location_id, @company.locations.order(:name), :id, :name, :include_blank => true %>
    </fieldset>
  <% end %>
  ...
  ...
<% end %>
class ItemVariant
item/new.html.erb

resources :companies do
  resources :items
end
class Item < ActiveRecord::Base
  attr_accessible :name, :sku, :item_type, :comments, :item_variants_attributes


  # Associations
  #-----------------------------------------------------------------------
  belongs_to :company
  belongs_to :item_type
  has_many   :item_variants

  accepts_nested_attributes_for :item_variants, allow_destroy: true

end
class ItemVariant < ActiveRecord::Base
  attr_accessible :item_id, :location_id

  # Associations
  #-----------------------------------------------------------------------
  belongs_to :item

end
<%= form_for [@company, @item] do |f| %>
  ...
  ...
  <%= f.fields_for :item_variants do |builder| %>
    <fieldset>
      <%= builder.label :location_id %>
      <%= builder.collection_select :location_id, @company.locations.order(:name), :id, :name, :include_blank => true %>
    </fieldset>
  <% end %>
  ...
  ...
<% end %>

...
...
正确%>
...
...
试试这种方法

项目控制器中
新操作
写入

def new
  ...
    @item = # define item here
    @item.item_variants.build if @item.item_variants.nil?
  ...
end
item/new.html.erb中

<%= form_for @item do |f| %>
  ...
  ...
  <%= f.fields_for :item_variants do |builder| %>
    ...
  <% end %>
  ...
  ...
<% end %>

...
...
...
...
...

有关更多信息,请参见视频-

您应该使用一些数据预填充
@item.item\u变体

def new # in the ItemController
  ...
  @item = Item.new
  3.times { @item.item_variants.build }
  ...
end

来源:

谢谢!工作完美,但作为参考,我使用的是修订后的RailsCast ep 196嵌套模型表格。在他的新行动中,它只包含了
@survey=survey.new
,没有建立协会。知道我为什么需要建立协会而Ryan没有/没有吗?railscast甚至更好,尽管很复杂,因为它可以在调查表单中动态添加问题。所以它不需要预先填充任何内容。我只是想为那些对.build和Ryans不需要它的原因感到困惑的人添加一些含义。如果您在第3行的appliance\u helper中查看他的代码,它将创建类实例。因此,这取代了。build。希望这对其他人有帮助!谢谢让它工作,但如果@item.item\u variants.nil?
不工作,则使用
@item.item\u variants.build。只有删除if语句,它才有效。而且,使用你的链接的修订版让我很困惑。在他的控制器中,他不使用.build。他只创建@survey实例。知道我为什么需要建立协会而Ryan没有/没有吗?