我目前正在尝试修复delivery,delivery_orders和订单模型之间的关系。我一直在努力保存一个嵌套表单,该表单存储delivery_orders表中的订单。基本上,我希望能够创建交货,然后通过delivery_order模型向交货添加订单。普通用户创建订单,但经理处理这些订单的交货。到目前为止,我一直无法为delivery_order模型保存嵌套的simple_form_for部分。即使关系设置为支持事务,它也会继续返回nil。一方面注意,我无法通过交付模型读取订单的属性。我写的代码如下。结构用户 - >订单 - >经理交货[Delivery_order] - >卡车[交货]
delivery.rb
class Delivery < ApplicationRecord
belongs_to :warehouse, optional: true
belongs_to :truck, optional: true
has_one :delivery_order
has_one :order_tracker
has_many :breads
#after_create :decrement_bread_stock
accepts_nested_attributes_for :delivery_order
end
deliveryorder.rb
class DeliveryOrder < ApplicationRecord
has_many :orders, inverse_of: :delivery_order
belongs_to :delivery, optional: true
end
order.rb
class Order < ApplicationRecord
belongs_to :client, :dependent => :destroy, optional: true
belongs_to :delivery_order, optional: true
has_one :bread
has_one :order_tracker, :dependent => :destroy
after_create :build_order_tracker
geocoded_by :address, :latitude => :lat, :longitude => :lon
after_validation :geocode, if: ->(obj) {obj.client_address.present? && obj.client_address_changed?}
def address
[client_address, client_state].compact.join(', ')
end
end
deliveries_controller.rb
class DeliveriesController < ApplicationController
before_action :set_delivery, only: %i[show edit update destroy]
# GET /deliveries
# GET /deliveries.json
def index
@deliveries = Delivery.all
end
# GET /deliveries/1
# GET /deliveries/1.json
def show; end
# GET /deliveries/new
def new
@delivery = Delivery.new
@delivery.build_delivery_order
@delivery_orders = Order.all
end
# GET /deliveries/1/edit
def edit; end
# POST /deliveries
# POST /deliveries.json
def create
@delivery = current_manager.warehouse.deliveries.build(delivery_params)
respond_to do |format|
if @delivery.save
format.html { redirect_to @delivery, notice: 'Delivery was successfully created.' }
format.json { render :show, status: :created, location: @delivery }
else
format.html { render :new }
format.json { render json: @delivery.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /deliveries/1
# PATCH/PUT /deliveries/1.json
def update
respond_to do |format|
if @delivery.update(delivery_params)
format.html { redirect_to @delivery, notice: 'Delivery was successfully updated.' }
format.json { render :show, status: :ok, location: @delivery }
else
format.html { render :edit }
format.json { render json: @delivery.errors, status: :unprocessable_entity }
end
end
end
# DELETE /deliveries/1
# DELETE /deliveries/1.json
def destroy
@delivery.destroy
respond_to do |format|
format.html { redirect_to deliveries_url, notice: 'Delivery was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_delivery
@delivery = Delivery.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def delivery_params
params.require(:delivery).permit(:delivery_date, :delivery_on_time, delivery_order_attributes: %i[order_1 order_2 order_3 order_4 delivery_id order_id], orders_attributes: [:order_id])
end
end
delivery _form.html.erb
<div class="container">
<%= simple_form_for(@delivery) do |f| %>
<%= f.error_notification %>
<div class="form-group">
<label>Estimated Delivery Date</label>
<%= f.input :delivery_date, class: 'form-control', label: false %>
</div>
<%= simple_fields_for :delivery_order, @delivery.delivery_order do |d| %>
<div class="form-control">
<label>Delivery Order 1</label>
<%= d.input :order_1, collection: @delivery_orders, :label_method => lambda {|order| "Order Id: #{order.id} | Client Name: #{order.client_name} | Client Address: #{order.client_address} | Bread Type: #{order.bread_type} | Bread Quantity: #{order.bread_quantity}" }, value_method: :id, include_blank: false, prompt: 'Add your first order to the delivery', class: 'form-control', label: false %>
</div>
<div class="form-group">
<label>Delivery Order 2</label>
<%= d.input :order_2, collection: @delivery_orders, :label_method => lambda {|order| "Order Id: #{order.id} | Client Name: #{order.client_name} | Client Address: #{order.client_address} | Bread Type: #{order.bread_type} | Bread Quantity: #{order.bread_quantity}" }, value_method: :id, include_blank: false, prompt: 'Add your second order to the delivery', class: 'form-control', label: false %>
</div>
<div class="form-group">
<label>Delivery Order 3</label>
<%= d.input :order_3, collection: @delivery_orders, :label_method => lambda {|order| "Order Id: #{order.id} | Client Name: #{order.client_name} | Client Address: #{order.client_address} | Bread Type: #{order.bread_type} | Bread Quantity: #{order.bread_quantity}" }, value_method: :id, include_blank: false, prompt: 'Add your third order to the delivery', class: 'form-control', label: false %>
</div>
<div class="form-group">
<label>Delivery Order 4</label>
<%= d.input :order_4, collection: @delivery_orders, :label_method => lambda {|order| "Order Id: #{order.id} | Client Name: #{order.client_name} | Client Address: #{order.client_address} | Bread Type: #{order.bread_type} | Bread Quantity: #{order.bread_quantity}" }, value_method: :id, include_blank: false, prompt: 'Add your fourth order to the delivery', class: 'form-control', label: false %>
</div>
<% end %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
</div>
订单_form.html.erb
<%= simple_form_for(@order) do |f| %>
<%= f.error_notification %>
<div class="form-group">
<label>Name</label>
<%= f.input :client_name, required: true, label: false, class: 'form-control' %>
</div>
<div class="form-group">
<label>Zip Code</label>
<%= f.input :client_zip_code, required: true, label: false, class: 'form-control' %>
</div>
<div class="form-group">
<label>Address</label>
<%= f.input :client_address, required: true, label: false, class: 'form-control' %>
</div>
<div class="form-group">
<label>State</label>
<%= f.input :client_state, required: true, label: false, class: 'form-control' %>
</div>
<div class="form-group">
<label>Choose a Bread</label>
<%= f.collection_select :bread_name, collection: Bread.all, required: true, label: false, class: 'form-control' %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
trucks_controller.rb
class TrucksController < ApplicationController
before_action :set_truck, only: %i[show edit update destroy]
before_action :authenticate_manager!
# GET /trucks
# GET /trucks.json
def index
@trucks = Truck.all
end
# GET /trucks/1
# GET /trucks/1.json
def show;
end
# GET /trucks/new
def new
@truck = Truck.new
@deliveries = Delivery.all
end
# GET /trucks/1/edit
def edit;
end
# POST /trucks
# POST /trucks.json
def create
@truck = current_manager.warehouse.trucks.build(truck_params)
respond_to do |format|
if @truck.save
format.html {redirect_to @truck, notice: 'Truck was successfully created.'}
format.json {render :show, status: :created, location: @truck}
else
format.html {render :new}
format.json {render json: @truck.errors, status: :unprocessable_entity}
end
end
end
# PATCH/PUT /trucks/1
# PATCH/PUT /trucks/1.json
def update
respond_to do |format|
if @truck.update(truck_params)
format.html {redirect_to @truck, notice: 'Truck was successfully updated.'}
format.json {render :show, status: :ok, location: @truck}
else
format.html {render :edit}
format.json {render json: @truck.errors, status: :unprocessable_entity}
end
end
end
# DELETE /trucks/1
# DELETE /trucks/1.json
def destroy
@truck.destroy
respond_to do |format|
format.html {redirect_to trucks_url, notice: 'Truck was successfully destroyed.'}
format.json {head :no_content}
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_truck
@truck = Truck.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def truck_params
params.require(:truck).permit(:delivery_total, :lon, :lat, :delivery_id, :loaded_date, :truck_driver_name, :current_street_address, :current_city, :current_state, :current_country)
end
end
truck.rb
class Truck < ApplicationRecord
belongs_to :warehouse, optional: true
has_many :deliveries
end
货车_form.html.erb
<div class="container">
<%= simple_form_for(@truck) do |f| %>
<%= f.error_notification %>
<div class="form-group">
<div class="col-3">
<label>Loaded Date (Click Box Below)</label>
<%= f.input :loaded_date, as: :date_time_picker, class: 'form-control', placeholder: "Tap to view calendar <i class: 'fa fa-hand-o-up'></i>", label: false %>
</div>
</div>
<div class="form-group">
<div class="col-5">
<%= f.input :delivery_total, class: 'form-control' %>
</div>
</div>
<div class="form-group">
<div class="col-5">
<%= f.input :truck_driver_name, class: 'form-control btn btn-outline-primary' %>
</div>
</div>
<div class="form-group">
<div class="col-5">
<%= f.input :current_street_address, class: 'form-control' %>
</div>
</div>
<div class="form-group">
<div class="col-5">
<%= f.input :current_city, class: 'form-control' %>
</div>
</div>
<div class="form-group">
<div class="col-5">
<%= f.input :current_state, class: 'form-control' %>
</div>
</div>
<div class="form-group">
<div class="col-5">
<%= f.input :current_country, class: 'form-control' %>
</div>
</div>
<div class="form-group">
<div class="col-5">
<%= f.input :delivery_id, collection: @deliveries, :label_method => lambda {|delivery| "Estimated Delivery Date: #{delivery.delivery_date} | Order Id: #{delivery.id}"}, value_method: :id, label: "Delivery Jobs", include_blank: false, prompt: 'Add a delivery to the truck' %>
</div>
</div>
<div class="form-actions">
<%= f.button :submit, class: 'btn btn-outline-success' %>
</div>
<% end %>
</div>