不工作的领域有很多协会

时间:2013-09-03 15:01:36

标签: ruby-on-rails forms associations ruby-on-rails-4 fields-for

我目前正在为模型构建表单,并且无法为表单开发相关的模型表单。

我收到的错误:

undefined method `tradie_id' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_TradieCategory:0xd0f9d78>

表单代码如下:

= form_for @data[:tradie], url: { action: "update_tradie" } do |tradie|

  # This doesn't work
  = tradie.fields_for :tradie_categories, @data[:tradie].tradie_categories do |category|
    = category.text_field :tradie_id

  # However this fields_for does
  = tradie.fields_for :tradie_locations, @data[:tradie].tradie_locations do |location|
    = location.text_field :address

Tradie模型与has_many模型有TradieCategory关系,TradieCategory模型与belongs_toTradie关系。我还确认TradieCategory有一个字段tradie_id

在上面的表单中,@data[:tradie]等于Tradie模型的实例。每当我致电tradie_categoriestradie_locations时,他们都会调用TradieCategoryTradieLocation的相关模型数据。

最后,在我的视图代码中,如果我运行= @data[:tradie].tradie_categories.to_a,我会在我的html页面上返回

[#<TradieCategory id: 2, tradie_id: 2, category_id: 1, created_at: "2013-09-08 19:50:10", updated_at: "2013-09-08 19:50:10">]

我不知道这有什么不妥。问题是什么?如何解决这个问题?

1 个答案:

答案 0 :(得分:6)

您是否在accepts_nested_attributes_for :tradie_categories型号中添加了Tradie