Also was ich versuche zu tun ist dies. Ich habe derzeit ein Gerät, das viele Teile hat. Ich habe auch ein Inventar von Teilen. Was ich tun möchte, ist, wenn Sie ein Teil zum Gerät hinzufügen, es aus dem Inventar entfernt. Ich habe derzeit die Teile, die an die Ausrüstung anschließen, aber nicht sicher, wie man die Quantität verringert. Hier sind die Admin-Modelle für Teile und Ausrüstung.ActiveAdmin: Aktualisieren Sie ein anderes Modell nach dem Erstellen/Aktualisieren
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
Und hier sind die Modelle für jede
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
Jede Hilfe wird sehr geschätzt.
Sie könnten Active Callbacks verwenden möchten http://guides.rubyonrails.org/active_record_callbacks.html –
Ich schaute auf Callbacks, aber ich bin stecken herauszufinden, wie mit den Multiple-IDs umzugehen. – nickmc01