1
我正試圖在同一時間創建兩個對象。我擁有的最好方法是使用fields_for。關係是has_many。Rails fields_for將不會在我的內部看到我的字段
模型/ location.rb
attr_accessible :address, :customer_id, :event_id, :latitude, :longitude
belongs_to :customer
belongs_to :event
模型/ event.rb
attr_accessible :locations_attributes
has_many :locations, :dependent => :destroy
accepts_nested_attributes_for :locations
的形式有如下:
<%= form_for(@event) do |f| %>
...
<%= f.fields_for :locations do |e| %>
<%= e.text_field :longitude %>
<%= e.text_field :latitude %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我的字段將顯示不出來,我遵循本節http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for 的文檔has_many,並注意是否在我的f.fiel ds_for:地點將其更改爲單數字段將顯示,但我將無法創建它,因爲我不允許修改locations_attributes。
更新:
如果我單數它。我改變了我的事件模型
attr_accessible :description, :title, :location_attributes
的錯誤是這樣的
app/controllers/events_controller.rb:60:in `new'
app/controllers/events_controller.rb:60:in `create'
我的控制器是這樣
line 60: @event = Event.new(params[:event])
正如你所說的,它應該是'<%= f.fields_for:位置做| E | %>'不':locations'爲什麼你不能修復它? – Khaled
因爲我得到一個錯誤不能mass_assignment,如果我看看它的文件也說明它有plurials – Jseb
添加location_attributes到模型 – msdq