【发布时间】:2014-05-14 19:56:28
【问题描述】:
我是 Rails 新手,我正在尝试创建一个新事件,允许用户从数据库中已存在的地点和组织中选择地点和组织。我正在通过关联使用 has_many。现在我无法保存活动,即使我尝试在没有 place_id 或 organization_id 字段的情况下保存活动。
型号:
class Event < ActiveRecord::Base
belongs_to :organization
belongs_to :place
end
class Organization < ActiveRecord::Base
has_many :events
has_many :places, through: :events
end
class Place < ActiveRecord::Base
has_many :events
has_many :organizations, through: :events
end
事件控制器:
def new
@event = Event.new
end
def create
@event = Event.new(event_params)
if @event.save
render 'show'
else
render 'new'
end
end
def event_params
params.require(:event).permit(:name, :description, :contact, :tag_list, :address, :latitude, :longitude, :long_description, :event_date, :start_time, :end_time, :organization_id, :place_id)
end
新事件视图:
<%= form_for @event do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :event_date %>
<%= f.text_field :event_date %>
<%= f.label :start_time %>
<%= f.text_field :start_time %>
<%= f.label :end_time %>
<%= f.text_field :end_time %>
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.label :contact %>
<%= f.text_field :contact %>
<%= f.label :address %>
<%= f.text_field :address %>
<%= f.label :long_description %>
<%= f.text_field :long_description %>
<%= f.label :tag_list, "Tags (separated by commas)" %>
<%= f.text_field :tag_list %>
<%= f.submit "Save event", class: "button" %>
<% end %>
我在这里缺少什么来保存事件,最好是组织和地点字段具有组织 ID 和地点 ID 的值?
【问题讨论】:
标签: ruby ruby-on-rails-4 has-many-through model-associations