我在填充相关对象时遇到了一些麻烦。
我有关联对象,我尝试用表单数据填充本地数据库表,我可以填充建筑物和自己组成的所有对象: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"}}}}
答案 0 :(得分:0)
您与 家具 和 阶段 之间存在关联has_many :points
,因此您需要更改
<%= ft.fields_for :point do |pt| %>
到
<%= ft.fields_for :points do |pt| %>
并在point_attributes
中将points_attributes
更改为buildings_params
。
尝试以下更改以及上述内容。
class Building < ActiveRecord::Base
has_many :stages, dependent: :destroy
has_many :furnitures, dependent: :destroy
has_one :light_reseller, dependent: :destroy
has_one :location, dependent: :destroy
accepts_nested_attributes_for :light_reseller, :location, :stages, :furnitures, :allow_destroy => true
end
def new
@building = Building.new
@building.build_location
@building.build_light_reseller
@building.furnitures.build
@building.stages.build
end
def buildings_params
params.require(:building).permit(:name, location_attributes:[:address, :city], light_reseller_attributes:[:name], furnitures_attributes:[:name, points_attributes:[:val]], stages_attributes:[:name, points_attributes:[:val]])
end
<%= f.fields_for :stage do |ft| %>
至<%= f.fields_for :stages do |ft| %>
和
<%= f.fields_for :furniture do |ft| %>
至<%= f.fields_for :furnitures do |ft| %>