【问题标题】:One to Many Nested Models form not saving一对多嵌套模型表格不保存
【发布时间】:2017-03-03 07:59:50
【问题描述】:

好的,所以我在一对一关系中遇到了这个问题,我认为这是一个与复数相似的问题。我已尝试输入 @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 %>

【问题讨论】:

  • 是否存在验证错误?如果你有,this will help
  • 来自您的问题@payment.profile_notes.build。你能告诉我profile_notes是什么吗?
  • 你的意思是迁移吗?
  • 您能说说您在控制器中尝试使用@payment.profile_notes.build 做什么吗?
  • 我正在尝试建立与表的关系,以便表单元素更新数据库。注释字段显示它只是不保存数据。我已经尝试过 @payment.profile_notes.build@payment.build_profile_notes 都没有工作。这能回答你的问题吗?

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


【解决方案1】:

在您的fields_for 中尝试:payment_notes_attributes

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多