33964

ActiveAdmin: Update another model after create/update

Question:

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.

<strong>equipment.rb</strong>

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

<strong>part.rb</strong>

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

And here are the models for each

<strong>models/equipment.rb</strong>

# == 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

<strong>models/part.rb</strong>

# == 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.

Answer1:

You can create a controller in activeadmin.

In 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

the same for equipment.rb. Hope it helps !

Recommend

  • Count occurrence of values in a serialized attribute(array) in Active Admin dashboard (Rails, Active
  • Best way to aggregate a list of items and collect totals
  • Javascript 2D array sorting - by numerical value
  • WebMethod returning generic list
  • Bind property to method
  • Laravel / Eloquent hasMany relationship sum()
  • Generating and downloading an excel file generates a ERR_INVALID_RESPONSE
  • AngularJS class ng-scope is not being added
  • Extending Boostrap to include extra small inline form
  • Django saving many to many entries using generic views
  • finding products that customers bought together
  • Is Couchbase Lite meant for Querying Data?
  • Duplicating records to fill gap between dates in Google BigQuery
  • NHibernate: Persist an object with children
  • Custom variables on product details page in Magento
  • magento success page variables
  • Full form of SKU in supportedRuntime in C#
  • VUE JS 2 + WEBPACK Cannot read property 'get' of undefined VUE RESOURCE
  • Sharepoint change Content Type in Sharepoint Designer using workflow
  • git post-receive hook to update multiple servers
  • Move an object (element) one step up with Javascript
  • NSOpenPanel's setDirectoryURL doesn't work on Lion
  • Windows Forms switch between Panels
  • Using Select and where statement in Criteria
  • Trying to total columns for relation datasource
  • Why is it still possible to insert a foreign key that doesn't exist?
  • Getting data from database using HQL
  • Extjs, handling success or failure when doing a standard submit in a form
  • How to merge two Request in Laravel
  • LINQ join with filter criteria
  • Python ImageIO Gif Set Delay Between Frames
  • Invalid Date on validation Date of js
  • How to disable all widgets inside Panel or inside Composite?
  • How integrated is Collada to OpenGL ES
  • How to write order and limit within cakephp joins array
  • Return words with double consecutive letters
  • Turn off referential integrity in Derby? is it possible?
  • Add sale price programmatically to product variations
  • Unable to use reactive element in my shiny app
  • How do I use LINQ to get all the Items that have a particular SubItem?