我几乎可以肯定我知道问题是什么,如果我是正确的,我就是找不到问题所在。
当用茧宝石进行三重嵌套时,我似乎有一个错误,即不正确的复数。我有3个资源,Developments
>Lots
>Listings
,其中Developments是祖父母,Lots是父母,Listings是孩子。
我得到的错误是源自ActiveModel::UnknownAttributeError in Developments#new
的unknown attribute 'development_id' for Lot.
。我检查了我的模型和零件,一直在摆弄它们。这是我的代码:
developments_controller.rb
class DevelopmentsController < ApplicationController
before_action :set_development, only: %i[ show edit update destroy ]
# GET /developments or /developments.json
def index
@developments = Development.all
end
# GET /developments/1 or /developments/1.json
def show
end
# GET /developments/new
def new
@development = Development.new
end
# GET /developments/1/edit
def edit
end
# POST /developments or /developments.json
def create
@development = Development.new(development_params)
respond_to do |format|
if @development.save
format.html { redirect_to @development, notice: "Development was successfully created." }
format.json { render :show, status: :created, location: @development }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @development.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /developments/1 or /developments/1.json
def update
respond_to do |format|
if @development.update(development_params)
format.html { redirect_to @development, notice: "Development was successfully updated." }
format.json { render :show, status: :ok, location: @development }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @development.errors, status: :unprocessable_entity }
end
end
end
# DELETE /developments/1 or /developments/1.json
def destroy
@development.destroy
respond_to do |format|
format.html { redirect_to developments_url, notice: "Development was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_development
@development = Development.find(params[:id])
end
# Only allow a list of trusted parameters through.
def development_params
params.require(:development).permit(:development_name, :development_type, :development_address, :description, :estimated_completion_date, :body_corp,
lots_attributes: [:id, :status, :stage, :land_size, :price, :eta, :_destroy,
listings_attributes: [:id, :lot_number, :price, :type, :bed, :bath, :car, :house_size, :rent, :_destroy]])
end
end
development.rb(模型(
class Development < ApplicationRecord
has_many :lots
accepts_nested_attributes_for :lots, reject_if: :all_blank, allow_destroy: :true
end
Lot.rb(模型(
class Lot < ApplicationRecord
has_many :listings
accepts_nested_attributes_for :listings, reject_if: :all_blank, allow_destroy: :true
belongs_to :development
end
Listing.rb(型号(
class Listing < ApplicationRecord
belongs_to :lot
end
_form.html.erb(部分(
<%= form_for @development do |f| %>
<div class="field">
<%= f.label :development_name%>
<%= f.text_field :development_name%>
</div>
<div class="field">
<%= f.label :development_type %>
<%= f.text_field :development_type %>
</div>
<div class="field">
<%= f.label :development_address %>
<%= f.text_field :development_address %>
</div>
<div class="field">
<%= f.label :description %>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :estimated_completion_date %>
<%= f.date_select :estimated_completion_date %>
</div>
<div class="field">
<%= f.label :body_corp %>
<%= f.number_field :body_corp %>
</div>
<div id="listings">
<%= f.fields_for :lots do |lot| %>
<%= render 'lot_fields', f: lot %>
<% end %>
<div class="links">
<%= link_to_add_association 'add lot', f, :lots %>
</div>
</div>
<%= f.submit %>
<% end %>
_lot_fields.html.erb(部分(
<div class="nested-fields">
<h3> New Lots </h3>
<div class="field">
<%= f.label :status %>
<br/>
<%= f.text_field :status %>
</div>
<div class="field">
<%= f.label :stage %>
<br/>
<%= f.text_field :stage %>
</div>
<div class="field">
<%= f.label :land_size %>
<br/>
<%= f.text_field :land_size %>
</div>
<div class="field">
<%= f.label :price %>
<br/>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :eta %>
<br/>
<%= f.text_field :eta %>
</div>
<%= link_to_remove_association "remove lot", f %>
</div>
_listing_fields.html.erb(部分(
<div class="nested-fields">
<h3> New Listing </h3>
<div class="field">
<%= f.label :status %>
<br/>
<%= f.text_field :status %>
</div>
<div class="field">
<%= f.label :stage %>
<br/>
<%= f.text_field :stage %>
</div>
<div class="field">
<%= f.label :land_size %>
<br/>
<%= f.text_field :land_size %>
</div>
<div class="field">
<%= f.label :price %>
<br/>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :eta %>
<br/>
<%= f.text_field :eta %>
</div>
<%= link_to_remove_association "remove listing", f %>
</div>
new.html.erb
<h1>New Development</h1>
<%= render 'form', development: @development %>
<%= link_to 'Back', developments_path %>
我有一个克隆项目,我在schema.rb
中注意到有一个关键区别:
t.index ["developments_id"], name: "index_lots_on_developments_id"
我认为它应该是development_id
,而不是多元的,但我不确定在哪里/如何改变它。我觉得你永远不应该更改模式文件。
非常感谢您的帮助!
确切地说,它应该是:t.index ["development_id"], name: "index_lots_on_development_id"
你必须用重写迁移
def change
remove_index :lots, name: "index_lots_on_developments_id"
add_reference :lots, :development, index: true
end