[英]Rails fields_for won't see my field inside
I am trying to create two object at the same time. 我正在尝试同时创建两个对象。 The best approach I have is to used fields_for. 我最好的方法是使用fields_for。 and the relationship is has_many. 关系是has_many。
model/location.rb 模型/ location.rb
attr_accessible :address, :customer_id, :event_id, :latitude, :longitude
belongs_to :customer
belongs_to :event
model/event.rb 模型/event.rb
attr_accessible :locations_attributes
has_many :locations, :dependent => :destroy
accepts_nested_attributes_for :locations
The form is has follow: 形式如下:
<%= 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 %>
My fields won't show up, I have followed the documentation has_many from this section http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for and notice if in my f.fields_for :locations change it to singular than the fields will show up but i won't be able to create it because i am not allow to modify the locations_attributes. 我的字段不会显示,我已遵循本节中的文档has_many http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-fields_for并注意是否在我的f.fields_for中:locations将其更改为单数,而不显示字段,但我无法创建它,因为我不允许修改locations_attributes。
UPDATE: 更新:
If i singular it. 如果我单数。 I change this to my event model 我将其更改为事件模型
attr_accessible :description, :title, :location_attributes
The error is like this 错误是这样的
app/controllers/events_controller.rb:60:in `new'
app/controllers/events_controller.rb:60:in `create'
My controller is like this 我的控制器就是这样
line 60: @event = Event.new(params[:event])
You should be doing this in your form: (location not locations) 您应该以表格的形式执行此操作:(位置而不是位置)
<%= f.fields_for :location do |e| %>
<%= e.text_field :longitude %>
<%= e.text_field :latitude %>
<% end %>
and in your model event.rb 并在您的模型中event.rb
attr_accessible :description, :title, :locations_attributes
(locations not location) attr_accessible :description, :title, :locations_attributes
(位置而非位置)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.