1

I have the following associations:

class Campaign < ActiveRecord::Base
  has_many :events
end

class Event < ActiveRecord::Base
  belongs_to :campaign
  belongs_to :venue
  accepts_nested_attributes_for :venue
end

class Venue < ActiveRecord::Base
  has_many :events
end

And I have the following form:

<%= form_for([@campaign, @event]) do |f| %>
  <%= f.fields_for :venue do |v| %>
    <%= v.text_field :search %>
  <% end %>
<% end %>

Anything in the fields_for block is not showing up. But the funny thing is if i change f.fields_for :venue to f.fields_for :venues, the :search field shows up.

But :venues shouldn't be plural on :events. Even in rails console Event.venues does not work

NoMethodError: undefined method `venues'

It also generates the wrong field names

This makes me think I have my model associations jacked up, but after reviewing the docs. it does not seem so. Here is my schema for reference:

  create_table "campaigns", force: true do |t|
    t.integer  "user_id"
    t.string   "title"
    t.text     "description"
    t.integer  "image_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "events", force: true do |t|
    t.integer  "campaign_id"
    t.datetime "start_date"
    t.datetime "end_date"
    t.integer  "venue_id"
    t.integer  "image_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "title"
    t.text     "description"
  end

  create_table "venues", force: true do |t|
    t.float    "latitude"
    t.float    "longitude"
    t.string   "address1"
    t.string   "address2"
    t.string   "city"
    t.string   "zip"
    t.string   "state"
    t.string   "country"
    t.string   "name"
    t.string   "gid"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
Vigrond
  • 8,148
  • 4
  • 28
  • 46

1 Answers1

1

See the similar SO Posts

1.Does accepts_nested_attributes_for work with belongs_to?

2.Getting fields_for and accepts_nested_attributes_for to work with a belongs_to relationship

As mentioned in one of those posts,one way of achieving it is changing the accepts_nested_attributes_for to the has_many side

class Campaign < ActiveRecord::Base
  has_many :events
  accepts_nested_attributes_for :events
end

class Event < ActiveRecord::Base
  belongs_to :campaign
  belongs_to :venue

end

class Venue < ActiveRecord::Base
  has_many :events
  accepts_nested_attributes_for :events
end

If it is not,you might want to build a venue for events with @event.build_venue in the controller before calling fields_for.

Hope it helps!

Community
  • 1
  • 1
Pavan
  • 33,316
  • 7
  • 50
  • 76
  • It does work for belongs_to, at least in rails4. But the 2nd answer of your 2nd link is the correct answer. I needed to call @event.build_venue before fields_for knew how to pick it up. My associations were correctly set up after all. If you want to rearrange your answer I'll mark it as the correct one. – Vigrond May 11 '14 at 07:06
  • @Vigrond I think now it looks ok.Anyways glad you figured it out. – Pavan May 11 '14 at 07:17