-1

我似乎无法使用嵌套表单,我要么看不到它,要么不理解它。这里的错误

Can't mass-assign protected attributes: location

现在在这里我是如何写我的

模型/事件:

#Data
  attr_accessible :customer_id, :description, :location_attributes
#Relationship
  has_many :locations
  accepts_nested_attributes_for :locations, :allow_destroy => true

型号/地点:

#Data
  attr_accessible :address, :customer_id, :event_id, :latitude, :longitude
#Relationship
  belongs_to :customer
  belongs_to :event

控制器:

  def create
    @event = current_customer.events.build(params[:event])
    ...

看法:

  <%= f.fields_for :location do |e| %>
    <%= e.hidden_field :longitude %>
    <%= e.hidden_field :latitude %>
  <% end %>

参数

 "location"=>{"longitude"=>"-80.9449995",
 "latitude"=>"46.435371599999996"},

我在 Rails 3.2.9 上并托管在 vps 服务器上。现在我不明白为什么它不起作用。

4

1 回答 1

1

你的形式应该是,并注意复数:

<% f.fields_for :locations do |location_form| %>

这是因为您正在为与表单对象关联的位置对象呈现字段。这将自动将这些字段的参数命名为location_attributes,然后将它们传递给您的控制器,然后您的模型将接受它们。

于 2013-01-04T03:49:09.400 回答