我不确定如何正确创建rails表单与嵌套表单。我遵循了许多教程,但越来越困惑应该是什么,单数复数,控制器……这里是我的模型
模型/event.rb attr_accessible :description :title, :tag_ids, :locations_attributes
has_many :location
accepts_nested_attributes_for :location, :allow_destroy => true
模型/location.rb attr_accessible :address, :customer_id, :event_id, :latitude, :longitude
belongs_to :customer
belongs_to :event
controller.rb
def new
@event = Event.new
...
def create
@event = Event.new(params[:event])
...
视图form.html.erb <%= form_for(@event) do |f| %>
<%= f.fields_for :locations do |e| %>
<%= e.text_field :longitude %>
<%= e.text_field :latitude %>
<% end %>
...
<% end %>
误差Can't mass-assign protected attributes: locations
参数发送
"locations"=>{"longitude"=>"45.6666",
"latitude"=>"47.44444665"},
要么我的关系是错误的,因为fields_for不支持它,要么我的控制器是不合适的,要么rails只是不是一个伟大的语言,或者我不理解它了
你。你马上就到了…
事件。rb - locations NOT location
attr_accessible :description :title, :tag_ids, :locations_attributes
has_many :locations
accepts_nested_attributes_for :locations, :allow_destroy => true
我认为应该这样做
编辑
正如Valery Kvon所说,你需要添加
@event.locations.build
到控制器
爱德华的回答+
def new
@event = Event.new
@event.locations.build
end