所以我想做的就是这个。我目前有一件设备上有很多部件。我也有零件库存。我要做的是当你向设备添加一个零件时,它会将它从库存中移除。我目前有部件连接到设备但不知道如何减少数量。以下是零件和设备的管理模型。
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
以下是每个
的模型模型/ 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
模型/ 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
非常感谢任何帮助。
答案 0 :(得分:1)
您可以在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来说是一样的。希望它有所帮助!