Rails 5错误消息:子模型父模型必须存在 [英] Rails 5 error message: Child-Model Parent-Model must exist

查看:61
本文介绍了Rails 5错误消息:子模型父模型必须存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型,父母是财产,孩子是电话.尝试使用嵌套的Phone数据创建新的Property记录时,我收到一条错误消息: Phones属性必须存在.

I have two models, Parent is Property, child is Phone. When attempting to create a new Property record with nested Phone data, I receive an error message: Phones property must exist.

我研究了《 Rails指南》和许多其他文档,但未确定原因.如果您想查看所有代码,这是一个公共github链接: https://github.com/allenroulston/testnest.git

I've studied the Rails Guide and a number of other documents without determining the cause. Here is a public github link if you want to see all the code: https://github.com/allenroulston/testnest.git

class Property < ApplicationRecord
  has_many :phones
  accepts_nested_attributes_for :phones
end

class Phone < ApplicationRecord
  belongs_to :property
end

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

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

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

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

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

  <div class="field">
    <%= f.label "Telephone (example: 613 555 1234 )" %>
    <%= f.fields_for :phones do |p| %>
      Area Code <%= p.text_field :area %>
      Exchange <%= p.text_field :exchange %>
      Number <%= p.text_field :number %>
    <% end %>
  </div>

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


 # relevant controller methods ##################

 # GET /properties/new
  def new
    @property = Property.new
    @property.phones.build
  end

  # POST /properties
  # POST /properties.json
  def create
    @property = Property.new(property_params)

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

推荐答案

据我所知,这是因为当您在创建新对象时使用nested_attributes_for时,尚未创建父对象,因此在尝试创建父对象时引用父对象,验证将失败.要解决此问题,您应该更改为:has_many :phones, inverse_of: :property.

As far as I know, this is because when you use nested_attributes_for when creating a new object, the parent object is not yet created, so when attempting to create a reference to the parent object, the validation fails. To fix this you should change to: has_many :phones, inverse_of: :property.

这篇关于Rails 5错误消息:子模型父模型必须存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆