表单中的嵌套属性不可见

时间:2015-02-16 22:51:29

标签: ruby-on-rails activeadmin nested-attributes formtastic

我可能会遗漏一些简单的事情,但我已经尝试了很多事情无济于事。 我需要能够编辑现有曲目(使用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 %>

当我去编辑一首曲目时,Track attrs就在那里,但是嵌套(Exclusivity attrs)不是(我可以查看曲目的独家和完全独立复选框)

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

我在这里缺少什么?

3 个答案:

答案 0 :(得分:0)

<%= 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

答案 1 :(得分:0)

您忘记将表单构建器传递给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 %>

答案 2 :(得分:0)

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

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