简体   繁体   中英

How can I use Active Admin's form block to update a table that tracks when an attribute in a resource is updated?

How can I use Active Admin's form block to update a table that tracks when a particular attribute in the current resource is updated?

For instance, I have something like this:

form do |f|
    f.inputs "Details" do
    f.input :name
    f.input :address
    f.input :zip
    f.input :city
    f.input :active_state, as: :select, collection: active_states
    f.input :approval_state, as: :select, collection: approval_states
    f.input :new_comment
  end
  f.buttons
end

where everything but new_comment is an attribute that exists on the current resource.

I tried doing something like this in the class for the resource:

def new_comment
  history = History.new(:id, Time.now)
  history.comment
end

I also tried creating a controller instance variable when that failed, but that didn't work either. The venue class doesn't really have any co-relation to the history class. I'm just trying to create an input column that will create a new row in my History table whenever the user edits a venue and then adds a comment.

The History model looks like this:

class History < ActiveRecord::Base
  attr_accessible :comment, :venue_id, :created_at
  belongs_to :venue

  def initialize(id, datetime)
    @id = id
    @datetime = datetime
  end
end

The form is for a Venue that looks like this:

class Venue < ActiveRecord::Base
 attr_accessible :name, :address, :zip, :city
 has_many :histories
 accepts_nested_attributes_for :histories
 //with a bunch of methods
end

I tried doing this after some digging but it didn't work either:

form do |f|
  f.inputs "Details" do
    f.input :name
    f.input :address
    f.input :zip
    f.input :city
    f.input :active_state, as: :select, collection: active_states
    f.input :approval_state, as: :select, collection: approval_states
  end
  f.inputs "History" do
    f.has_many :histories do |h|
        h.input :id
        h.input :comment
        h.input :modified_at
    end
  end
  f.buttons
end

In addition to setting up the model relationships and accepts_nested_attributes_for , try this in your ActiveAdmin resource:

form do |f|
  f.inputs "Details" do
    f.input :name
    f.input :address
    f.input :zip
    f.input :city
    f.input :active_state, as: :select, collection: active_states
    f.input :approval_state, as: :select, collection: approval_states
    f.has_many :histories do |h|
      h.input :id
      h.input :comment
      h.input :modified_at
    end
  end
  f.actions
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM