Question

I'm currently building a form for a model and am having trouble developing the associated model forms for the form.

The error I'm receiving:

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

The form code looks like:

= 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

The Tradie model has a has_many relationship with the TradieCategory model and the TradieCategory model has a belongs_to relationship with Tradie. I also verified that TradieCategory has a field tradie_id.

In the form above, @data[:tradie] equals the instance of the Tradie model. Whenever I call tradie_categories or tradie_locations they are calling the associated model data of TradieCategory and TradieLocation.

Finally, in my view code, if I run = @data[:tradie].tradie_categories.to_a, I'm returned on my html page

[#<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">]

I have no idea what could be wrong with this. What's the problem and how could I fix this?

Was it helpful?

Solution

Did you add accepts_nested_attributes_for :tradie_categories in the Tradie model?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top