0

I have an event model and a venue model. Each event has 1 venue. But a venue can have multiple events. As part of the event creation process I also want to capture info about the venue and store it. Right now I'm struggling with the nested form setup. I get the following error:

ActiveRecord::UnknownAttributeError in EventsController#new
unknown attribute: event_id

Event Model

class Event < ActiveRecord::Base
attr_accessible :name, :budget, :user_id, :venue_id, :client, :date, :description, 
:attendees, :assets_attributes, :tag_list, :venue_attributes
belongs_to :user

has_many :assets, :dependent => :destroy
has_many :vendors
has_one :venue

accepts_nested_attributes_for :assets, :allow_destroy => true
accepts_nested_attributes_for :vendors, :allow_destroy => true
accepts_nested_attributes_for :venue, :allow_destroy => true
acts_as_taggable
end

Venue Model

class Venue < ActiveRecord::Base
attr_accessible :capacity, :city, :contact, :country, :email, :exclusiveVendors,:fee, 
:latitude, :longitude, :name, :state, :street, :tel, :union
belongs_to :event
has_many :vendors
acts_as_gmappable
has_many :events
end

Event Controller

def new
  @event = Event.new
  5.times { @event.assets.build }
  venue = @event.build_venue()
  # respond_to do |format|
  #   format.html # new.html.erb
  #   format.xml  { render :xml => @event }
  # end

end

def create
  @user = current_user
  @event = Event.new(params[:event])
  @event.create_venue()

  5.times { @event.build_assets}
  if @event.save
    redirect_to @event, :notice => "Successfully created event."
  else
    render :action => 'new'
  end
end

Form (excerpt)

<%= f.fields_for :venue do |builder| %>

    <%= builder.label :name %><br />
    <%= builder.text_field :name %>

<% end %>

1 Answer 1

2

I think you got the relationship wrong here. The Venue Model should have has_many :events and Event model should have belongs_to :venue and a field named venue_id.

And get rid of has_one :venue from Event model and belongs_to :event from Venue model.

Sign up to request clarification or add additional context in comments.

4 Comments

that inserted a blank record but the venue name field does not get populated in the venues table, any ideas why that could be? Do I need to pass anything into @event.create_venue() ?
you need to pass the event object while creating the form. form_for(@event) do |f| f.fields_for(:venue) do |ff| # fields of venue here. end end
and get rid of the build_venue() and create_venue() methods from controller. Those are not needed when using nested attributes.
if i get rid of the build_venue() then the venue field doesn't show up in the form. Removing create_venue() did fix the issue though. Thanks a ton!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.