我有一个活动模型和一个场地模型。每个活动有1个场地。但是场地可以有多个活动。作为活动创建过程的一部分,我还想捕获有关场地的信息并存储它。现在我正在努力使用嵌套表单设置。我收到以下错误:
ActiveRecord::UnknownAttributeError in EventsController#new
unknown attribute: event_id
活动模型
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
场地模型
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
事件控制器
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
表格(摘录)
<%= f.fields_for :venue do |builder| %>
<%= builder.label :name %><br />
<%= builder.text_field :name %>
<% end %>
答案 0 :(得分:2)
我认为你这里的关系错了。场地模型应该有has_many:events和Event模型应该有belongs_to:venue和一个名为venue_id的字段。
从场景模型中删除has_one:场地,并从场地模型中删除belongs_to:event。