一对多嵌套模型不保存

时间:2017-03-03 07:59:51

标签: ruby-on-rails ruby ruby-on-rails-5 nested-forms nested-form-for

好的,所以我在一对一的关系中遇到了这个问题,我将会发现这是一个与复数相似的问题。我试图加入 @ payment.profile_notes.build 但它仍然无效。我已经允许模型中的所有内容,我相信关系设置正确。我不确定这里发生了什么,但我确信这是一个很小的东西,我很想念,因为我是一对一的关系。在此先感谢您的帮助!

付款方式

class Payment < ApplicationRecord
  belongs_to :user
  has_many :payment_notes, inverse_of: :payment
  accepts_nested_attributes_for :payment_notes, allow_destroy: true
end

付款备注型号*

class PaymentNote < ApplicationRecord
  belongs_to :payment, inverse_of: :payment_note
end

控制器

class PaymentsController < ApplicationController
before_action :set_payment, only: [:show, :edit, :update, :destroy]

# GET /payments
# GET /payments.json
def index
  @payments = Payment.all
end

# GET /payments/1
# GET /payments/1.json
def show
end

# GET /payments/new
def new
  @payment = Payment.new
  @payment.build.profile_notes
end

# GET /payments/1/edit
def edit
  @payment.profile_notes.build
end

# POST /payments
# POST /payments.json
def create
  @payment = Payment.new(payment_params)

  respond_to do |format|
    if @payment.save
      format.html { redirect_to @payment, notice: 'Payment was successfully created.' }
      format.json { render :show, status: :created, location: @payment }
    else
      format.html { render :new }
      format.json { render json: @payment.errors, status: :unprocessable_entity }
    end
  end
end

# PATCH/PUT /payments/1
# PATCH/PUT /payments/1.json
def update
  respond_to do |format|
    if @payment.update(payment_params)
      format.html { redirect_to @payment, notice: 'Payment was successfully updated.' }
      format.json { render :show, status: :ok, location: @payment }
    else
      format.html { render :edit }
      format.json { render json: @payment.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /payments/1
# DELETE /payments/1.json
def destroy
  @payment.destroy
  respond_to do |format|
    format.html { redirect_to payments_url, notice: 'Payment was successfully destroyed.' }
    format.json { head :no_content }
  end
end

private
  # Use callbacks to share common setup or constraints between actions.
  def set_payment
    @payment = Payment.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def payment_params
    params.require(:payment).permit(:user_id, :fee, :other_fee, :payment_amount, :payment_number, :payment_date, :total, :payment_type, payment_notes_attributes: [:id, :note])
  end
end

新视图

<h1>New Payment</h1>

<%= render 'form', payment: @payment %>

<%= link_to 'Back', payments_path %>

修改视图

<h1>Editing Payment</h1>

<%= render 'form', payment: @payment %>

<%= link_to 'Show', @payment %> |
<%= link_to 'Back', payments_path %>

表单部分

<%= form_for(payment) do |f| %>
  <% if payment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(payment.errors.count, "error") %> prohibited this payment from being saved:</h2>

      <ul>
      <% payment.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :user_id %>
    <%= f.text_field :user_id %>
  </div>

  <div class="field">
    <%= f.label :fee %>
    <%= f.text_field :fee %>
  </div>

  <div class="field">
    <%= f.label :other_fee %>
    <%= f.text_field :other_fee %>
  </div>

  <div class="field">
    <%= f.label :payment_amount %>
    <%= f.text_field :payment_amount %>
  </div>

  <div class="field">
    <%= f.label :payment_number %>
    <%= f.text_field :payment_number %>
  </div>

  <div class="field">
    <%= f.label :payment_date %>
    <%= f.date_select :payment_date %>
  </div>

  <div class="field">
    <%= f.label :total %>
    <%= f.text_field :total %>
  </div>

  <div class="field">
    <%= f.label :payment_type %>
    <%= f.text_field :payment_type %>
  </div>
    <h3>Notes</h3>
  <%= f.fields_for :payment_notes do |note| %>
    <div class="field">
      <%= note.text_field :note %>
    </div>
  <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

1 个答案:

答案 0 :(得分:0)

:payment_notes_attributes

中尝试fields_for