2016-10-10 24 views
0

これは私がやっていることです。私は現在、その上に多くの部品を持っている機器を持っています。私も部品の在庫があります。私がやってみたいことは、機器に部品を追加するときに、部品を在庫から取り除くときです。私は現在、機器に取り付けられている部品を持っていますが、量を減らす方法はわかりません。ここには、部品と機器の管理モデルがあります。ActiveAdmin:作成/更新後に別のモデルを更新する

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

アクティブレコードコールバックを使用したい場合があります。http://guides.rubyonrails.org/active_record_callbacks.html –

+0

コールバックを調べていましたが、複数のパーツIDの処理方法を把握していません。 – nickmc01

答えて

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に同じ。それが役に立てば幸い !

関連する問題