Поместив f.has_many на раннем этапе в форму ActiveAdmin, сделайте дубликат has_one ниже - PullRequest
0 голосов
/ 25 марта 2020

У меня есть следующие файлы моделей:

// app/models/account.rb
class Account < ApplicationRecord
  has_many :notes, inverse_of: :account
  has_one :configuration_setting, inverse_of: :account, dependent: :destroy

  accepts_nested_attributes_for :notes, :allow_destroy => true
  accepts_nested_attributes_for :configuration_setting

  def configuration_setting
    super || build_configuration_setting
  end
end


// app/models/note.rb
class Note < ApplicationRecord

  belongs_to :account, inverse_of: :notes, required: true

  ATTRIBUTES = [
    :id,
    :text_field,
    :_destroy
  ]
 end

// app/models/configuration_setting.rb
class ConfigurationSetting < ApplicationRecord
  belongs_to :account, inverse_of: :configuration_setting

  ATTRIBUTES = [ :is_enabled ]

  UPDATABLE_ATTRIBUTES = ATTRIBUTES + [:id]
 end

и этот файл формы ActiveAdmin:

// app/admin/accounts.rb
ActiveAdmin.register Account do
  permit_params :name,
                notes_attributes: Note::ATTRIBUTES,
                configuration_setting_attributes: ConfigurationSetting::UPDATABLE_ATTRIBUTES

form partial: "admin/accounts/new_and_edit_form"
end

, а также файл формы html:

// app/views/admin/accounts/_new_and_edit_form.html.erb
<% url = @account.id.nil? ? admin_accounts_path : admin_account_path %>
<% submit_button_label = @account.id.nil? ? "Create Account" : "Update Account" %>
<% method = @account.id.nil? ? :post : :patch %>
<%= semantic_form_for @account, method: method, url: url, builder: ActiveAdmin::FormBuilder  do |f| %>
<%= f.actions do %>
  <%= f.action :submit, label: submit_button_label %>
<% end %>
<%= f.inputs "Notes" do %>
   <% f.has_many :notes, heading: false, new_record: true, allow_destroy: true do |t| %>
     <% t.input :text_field %>
   <% end %>
<% end %>
 <%= f.inputs for: :configuration_setting do |s| %>
   <%= s.input :is_enabled %>
 <% end %>
 <%= f.inputs "Name" do %>
   <% f.input :name %>
 <% end %>
<% end %>

Ikk почему кажется, что если я поместил has_many выше has_one, он отобразит дубликат has_one, как на этом снимке экрана новой формы редактирования: enter image description here

Если я поставлю примечания под has_one форма, она не дублируется: enter image description here Я предполагаю, что перемещение по входам в форме - это базовая c вещь, которую нужно сделать, так почему бы это не сработать? есть ли правило для has_many, которое нужно поместить в конец формы? Спасибо

...