使用相同的表单创建Rails对象

时间:2016-05-15 01:28:49

标签: ruby-on-rails nested-forms

我正在构建一个管理债务的应用程序,并尝试使用相同的表单创建三个对象。

模型是;

  1. 债务(欠款)

    class Debt < ActiveRecord::Base
      belongs_to :user
      belongs_to :creditor
      belongs_to :debtor
      accepts_nested_attributes_for :debtor, :creditor
    end
    
  2. 债务人(欠款人)

    class Debtor < ActiveRecord::Base
      belongs_to :user
      has_many :debts
    end
    
  3. 债权人(被欠款的人)

    class Creditor < ActiveRecord::Base
       belongs_to :user
       has_many :debts
    end
    
  4. 我正在使用新的债务形式,并希望为债务人和债权人显示字段。因此,当创造新债务时,相关的债务人和债权人也是如此。

    我查看了文档,但无法获取债务人或债权人在表格上显示的字段。

    这是表格

     <%= simple_form_for(@debt) do |f| %>
     <%= f.error_notification %>
    
     <!-- Debt fields -->
     <div class="form-inputs">
       <%= f.association :user %>
       <%= f.input :amount %>
       <%= f.input :commission %>
       <%= f.input :invoice_issued %>
       <%= f.input :invoice_date %>
       <%= f.input :status %>
       <%= f.input :details %>
    </div>
    
    <!-- Debtor Fields -->
     <ul>
      <%= f.fields_for :debtor do |debtor_form| %>
       <li>
        <%= debtor_form.association :user %>
        <%= debtor_form.input :business_name %>
        <%= debtor_form.input :abn %>
        <%= debtor_form.input :first_name %>
        <%= debtor_form.input :last_name %>
        <%= debtor_form.input :email %>
        <%= debtor_form.input :mobile_number %>
        <%= debtor_form.input :phone_number %>
      </li>
     <% end %>
     </ul>
    
     <div class="form-actions">
      <%= f.button :submit %>
     </div>
     <% end %>
    

    感谢任何帮助。

    修改

    create_table "debts", force: :cascade do |t|
     t.integer  "user_id"
     t.float    "amount"
     t.float    "commission"
     t.boolean  "invoice_issued"
     t.date     "invoice_date"
     t.string   "status"
     t.text     "details"
     t.datetime "created_at",     null: false
     t.datetime "updated_at",     null: false
    

1 个答案:

答案 0 :(得分:1)

您需要在您的控制人中建立您债务的债务人或债权人:

img {
  width: 230px !important;
  height: 230px !important;
}