0
我有一些麻煩來填充我的關聯對象。在rails中初始化關聯對象
我有關聯的對象,我嘗試用表單數據填充本地數據庫的表格,我可以填充建築物和組成它自己的所有對象:light_reseller,舞臺,傢俱,位置。但我沒有找到如何填補舞臺和 傢俱的組成部分「點」!
這裏是我的模型building.rb
class Building < ActiveRecord::Base
has_many :stage, dependent: :destroy
has_many :furniture, dependent: :destroy
has_one :light_reseller, dependent: :destroy
has_one :location, dependent: :destroy
accepts_nested_attributes_for :light_reseller, :location, :stage, :furniture, :allow_destroy => true
end
class LightReseller < ActiveRecord::Base
belongs_to :building
end
class Location < ActiveRecord::Base
belongs_to :building
end
class Stage < ActiveRecord::Base
belongs_to :building
has_many :points, dependent: :destroy
accepts_nested_attributes_for :points
end
class Furniture < ActiveRecord::Base
belongs_to :building
has_many :points, dependent: :destroy
accepts_nested_attributes_for :points
end
class Point < ActiveRecord::Base
belongs_to :furniture
belongs_to :stage
end
我用一個表格填寫我的對象:
<%= form_for (@building) do |f| %>
<% if @building.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@building.errors.count, "error") %> prohibited this store from being
saved:</h2>
<ul>
<% @building.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<h2>Store</h2>
<%= f.label :name %><br>
<%= f.text_field :name, class: "form-control" %>
<%= f.fields_for :location do |loc| %>
<%= loc.label :address %><br>
<%= loc.text_field :address, class: "form-control" %>
<%= loc.label :city %><br>
<%= loc.text_field :city, class: "form-control" %>
<% end %>
<%= f.fields_for :light_reseller do |lr| %>
<%= lr.label :"light_reseller name" %><br>
<%= lr.text_field :name, class: "form-control" %>
<% end %>
<h2>Stage</h2>
<%= f.fields_for :stage do |ft| %>
<%= ft.label :name %><br>
<%= ft.text_field :name, class: "form-control" %>
<%= ft.fields_for :point do |pt| %>
<%= pt.label :"point value" %><br>
<%= pt.text_field :val, class: "form-control" %>
<% end %>
<h3>Stage entries</h3>
<%= ft.fields_for :entrie do |et| %>
<%= et.label :"entrie name" %><br>
<%= et.text_field :name, class: "form-control" %>
<%= et.fields_for :point do |ept| %>
<%= ept.label :"point value" %><br>
<%= ept.text_field :val, class: "form-control" %>
<% end %>
<% end %>
<% end %>
<%= f.fields_for :furniture do |ft| %>
<h2>Furniture</h2>
<%= ft.label :name %><br>
<%= ft.text_field :name, class: "form-control" %>
<%= ft.fields_for :point do |pt| %>
<%= pt.label :"point value" %><br>
<%= pt.text_field :val, class: "form-control" %>
<% end %>
<%= ft.fields_for :point do |pt| %>
<%= pt.label :"point value" %><br>
<%= pt.text_field :val, class: "form-control" %>
<% end %>
<% end %>
</div>
<div class="actions">
<%= f.submit "Add a Store", class: "btn btn-default"%>
</div>
<% end %>
,這裏是我的控制器:
class BuildingsController < ApplicationController
before_action :set_building, only: [:show, :edit, :update, :destroy]
def index
@buildings = Building.all
end
def new
@building = Building.new
@building.build_location
@building.build_light_reseller
@building.furniture.new
@building.stage.build
end
def show
end
def create
@building = Building.new(buildings_params)
respond_to do |format|
if @building.save
format.html { redirect_to @building, notice: 'building was successfully created.' }
format.json { render :index, status: :created, location: @building }
else
format.html { render :new }
format.json { render json: @building.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @building.update(buildings_params)
format.html { redirect_to @building, notice: 'building was successfully updated.' }
format.json { render :show, status: :ok, location: @buildings }
else
format.html { render :edit }
format.json { render json: @buildings.errors, status: :unprocessable_entity }
end
end
end
def destroy
@building.destroy
respond_to do |format|
format.html { redirect_to buildings_path, notice: 'building was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_building
@building = Building.find(params[:id])
end
def buildings_params
params.require(:building).permit(:name, location_attributes:[:address, :city], light_reseller_attributes:[:name], furniture_attributes:[:name, point_attributes:[:val]], stage_attributes:[:name, point_attributes:[:val]])
end
所以,當我提交我的表格,我的控制器收到這些數據:
"building"=>{"name"=>"Fnac", "location_attributes"=>{"address"=>"12 bird road", "city"=>"Paris"}, "light_reseller_attributes"=>{"name"=>"Marc"}, "stage_attributes"=>{"0"=>{"name"=>"First", "point"=>{"val"=>"1235"}}}, "furniture_attributes"=>{"0"=>{"name"=>"shelf", "point"=>{"val"=>"45"}}}}
我試圖改變你說什麼,但我的形式對舞臺的觀點和傢俱的點場,如果我更改爲不打印: <%= ft.fields_for:points do | pt | %> 我是否需要在某處像我傢俱一樣啓動此字段: @ building.furniture.build – golbargk
@golbargk請檢查我的更新答案。 – Pavan