表单中的嵌套属性不可见

Nested attributes in form not visible

我可能遗漏了一些简单的东西,但我尝试了很多东西都无济于事。 我需要能够编辑现有曲目(使用 ActiveAdmin)并保存更新其排他性状态的记录。

我有一个Tracks模型

#app/models/track.rb:

  has_many :exclusivities, class_name: 'Exclusivity', dependent: :destroy

  accepts_nested_attributes_for :exclusivities

  attr_accessible :exclusivities_attributes

  #whether or not track is exclusive [boolean]
  attr_accessible :exclusive

  #whether or not track is fully exclusive and should be pulled off the front end [boolean]
  attr_accessible :full_exclusivity

我刚刚创建了一个 Exclusivity 模型。

#app/models/exclusivity.rb    

  belongs_to :track
  attr_accessible :end_date, :industry, :track_id, :notes, :staff_notes

和我的表格:

<%= semantic_form_for [:manage, @track], html: {data: {secondary:true}} do |f| %>

a bunch of inputs.....

<%= f.inputs "Track Exclusivity", class:'inputs align-left' do %>
  <%= f.input :exclusive %>
  <%= f.input :full_exclusivity,
  label: "Fully Exclusive" %>
  <%= f.fields_for :exclusivities do %>
    <%= f.input :notes %> 
    <%= f.input :staff_notes %> 
    <%= f.input :industry %> 
    <%= f.input :end_date, as: :date_picker %>
  <% end %>
<% end %>

当我去编辑轨道时,轨道属性在那里,但嵌套(排他性属性)不存在(我可以检查轨道的排他性和 full_exclusivity 复选框)

我也尝试过使用 semantic_fields_forf.inputs :for => exclusivities

我在这里错过了什么?

<%= form_for @person do |person_form| %>
  ...
  <%= person_form.fields_for :projects do |project_fields| %>
    <% if project_fields.object.active? %>
      Name: <%= project_fields.text_field :name %>
    <% end %>
  <% end %>
  ...
<% end %>

来自 http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for

您忘记将表单生成器传递给 fields_for 块。

<%= f.inputs "Track Exclusivity", class:'inputs align-left' do %>
  <%= f.input :exclusive %>
  <%= f.input :full_exclusivity,
  label: "Fully Exclusive" %>
  <%= f.fields_for :exclusivities do |ff| %>
    <%= ff.input :notes %> 
    <%= ff.input :staff_notes %> 
    <%= ff.input :industry %> 
    <%= ff.input :end_date, as: :date_picker %>
  <% end %>
<% end %>

实际问题是:如果轨道的排他性属性为空,则不会显示表单字段。因此,只要您点击编辑控制器,我就会使用此代码自动构建关联的属性。成功了!

 def edit
   @track = Track.includes(:licenses).find(params[:id])
      if @track.exclusivities.empty?
        @track.exclusivities.build
      end
    @track.save
 end