问题描述
所以我想做的就是这个。我目前有一件设备,上面有很多零件。我也有零件清单。我要做的是将零件添加到设备后,将其从库存中删除。我目前有零件附在设备上,但不确定如何减少数量。这是零件和设备的管理模型。
So what I am trying to do is this. I currently have a piece of equipment that has many parts on it. I also have an inventory of parts. What I am looking to do is when you add a part to the equipment, it removes it from the inventory. I currently have the parts attaching to the equipment but not sure how to decrease the quantity. Here are the admin models for part and equipment.
equipment.rb
ActiveAdmin.register Equipment do
permit_params :name, :description, :status, :manufacturer_id, :serial, :category_id, :location_id, part_ids: []
config.per_page = 5
filter :status, as: :select, collection: proc { Equipment::STATUS }
filter :category, as: :select, collection: proc { Category.all.where('category_id > ?', 0) }
index do
column("Name", :sortable => :id) {|equipment| link_to "#{equipment.name} ", equipment_path(equipment) }
column("Status") {|item| status_tag(item.status,
if item.status == 'online'
:green
else
:red
end
)}
column :manufacturer
column :serial
column :category_id
column :location_id
actions
end
show do |equipment|
attributes_table do
row :name
row :description do |equipment_item|
raw(equipment_item.description)
end
row :location
row :status
row :serial
row :category
row :manufacturer
end
panel "Parts List" do
table_for equipment.parts do |parts|
column("Name", :sortable => :id) {|part| link_to "#{part.name} ", part_path(part) }
column :sku
column :location
column :department
column("Quantity In Stock", :qty)
end
end
active_admin_comments
end
form html: { multipart: true } do |f|
f.inputs do
f.input :name, label: "Equipment Name"
f.input :description, label: "Equipment Description", as: :html_editor
f.input :serial, label: "Serial"
f.input :status, :label => 'Status', :as => :select2, :input_html => { :style => 'width:80%' }, collection: Equipment::STATUS if current_user.role == 'admin'
f.input :manufacturer_id, :label => 'Manufacturer', :as => :select2, :input_html => { :style => 'width:80%' }, :collection => Manufacturer.all.map{|m| ["#{m.name}", m.id]}
f.input :location_id, :label => 'Location', :as => :select2, :input_html => { :style => 'width:80%' }, :collection => Location.all.map{|l| ["#{l.name}", l.id]}
f.input :category_id, :label => 'Category', :as => :select2, :input_html => { :style => 'width:80%' }, :collection => Category.all.map{|c| ["#{c.name}", c.id]}
f.input :parts, :label => 'Parts', :as => :select2_multiple, :input_html => { :style => 'width:80%' }, :collection => Part.all.map{|c| ["#{c.name}", c.id]}
end
f.actions
end
end
part.rb
ActiveAdmin.register Part do
permit_params :username, :name, :description, :sku, :part_url, :qty,
:attachment, :location_id, :department_id, :remove_attachment, :_wysihtml5_mode
filter :name
filter :location
filter :department
scope :all, :default => true
index do
column("Name", :sortable => :id) {|part| link_to "#{part.name} ", part_path(part) }
column :sku do |part|
best_in_place part, :sku, as: :input
end
column :qty do |part|
best_in_place part, :qty, as: :input,
:place_holder => "Click To Edit",
:html_attrs => { :style => 'width:30%' }
end
column :location
column :department
end
show do |parts|
attributes_table do
row :name
row :description do |part|
raw(part.description)
end
row :location
row :department
row :sku
row :qty
row("Attached Documents", :sortable => :id) {|part| link_to "#{part.attachment_identifier} ", part.attachment_url }
row("Part URL", :sortable => :id) {|part| link_to "#{part.part_url} ", part.part_url }
end
active_admin_comments
end
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs do
f.input :name, label: "Part Name"
f.input :description, label: "Part Description", as: :html_editor
f.input :sku, label: "Part SKU"
f.input :qty
f.input :part_url, label: "Part URL"
f.input :department_id, :label => 'Department', :as => :select2, :input_html => { :style => 'width:80%' }, :collection => Department.all.map{|d| ["#{d.name}", d.id]}
f.input :location_id, :label => 'Location', :as => :select2, :input_html => { :style => 'width:80%' }, :collection => Location.all.map{|l| ["#{l.name}", l.id]}
f.input :attachment, :as => :file, :hint => "Current Document: " +
if f.object.attachment_identifier
f.object.attachment_identifier
else
""
end
f.input :remove_attachment, as: :boolean, required: :false, label: 'Remove Document'
end
f.actions
end
end
这是每个模型的
models / equipment.rb
# == Schema Information
#
# Table name: equipment
#
# id :integer not null, primary key
# name :string
# description :text
# location_id :integer
# status :string
# serial :string
# created_at :datetime not null
# updated_at :datetime not null
# category_id :integer
# manufacturer_id :integer
#
class Equipment < ApplicationRecord
has_and_belongs_to_many :parts
belongs_to :location
belongs_to :category
belongs_to :manufacturer
has_paper_trail
STATUS = %w[online offline].freeze
def status?(base_status)
return false unless status
STATUS.index(base_status.to_s) <= STATUS.index(status)
end
def remove_part_from_equipment
equipment = Equipment.find(params[:post][:id])
part = equipment.parts.find(params[:part][:id])
if part
equipment.parts.delete(part)
end
end
end
models / part.rb
# == Schema Information
#
# Table name: parts
#
# id :integer not null, primary key
# name :string
# description :text
# location_id :integer
# sku :string
# created_at :datetime not null
# updated_at :datetime not null
# part_url :string
# document :string
# attachment :string
# department_id :integer
# qty :integer
#
class Part < ApplicationRecord
has_and_belongs_to_many :equipment
belongs_to :location
belongs_to :department
has_paper_trail
mount_uploader :attachment, AttachmentUploader
end
任何帮助都将不胜感激。
Any help is greatly appreciated.
推荐答案
您可以在activeadmin中创建一个控制器。
You can create a controller in activeadmin.
在part.rb中:
ActiveAdmin.register Part do
//the customisation of index, show, form
controller do
def create
@part = Part.new(permitted_params[:part])
// do what you want
if @part.save
redirect_to admin_parts_path
else
render :new
end
end
end
end
对于equipment.rb同样。希望对您有所帮助!
the same for equipment.rb. Hope it helps !
这篇关于ActiveAdmin:在创建/更新后更新另一个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!