0

好吧,所以我有這個問題與一對一的關係,我會認爲這是一個類似的問題與複數。我試圖把@ 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 %> 
+0

你有存在驗證錯誤嗎?如果你有,[這將幫助](https://github.com/thoughtbot/shoulda-matchers/issues/870) – radubogdan

+0

從你的問題'@ payment.profile_notes.build'。你能告訴我什麼是'profile_notes'嗎? –

+0

你的意思是遷移嗎? –

回答

0

嘗試:payment_notes_attributesfields_for