[英]Rails fields_for won't see my field inside
我正在嘗試同時創建兩個對象。 我最好的方法是使用fields_for。 關系是has_many。
模型/ 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 %>
我的字段不會顯示,我已遵循本節中的文檔has_many http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for並注意是否在我的f.fields_for中:locations將其更改為單數,而不顯示字段,但我無法創建它,因為我不允許修改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 :location do |e| %>
<%= e.text_field :longitude %>
<%= e.text_field :latitude %>
<% end %>
並在您的模型中event.rb
attr_accessible :description, :title, :locations_attributes
(位置而非位置)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.