[英]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? 如何使用Active Admin的表单块来更新跟踪当前资源中特定属性何时更新的表?
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. 其中,除了
new_comment
所有内容都是当前资源上存在的属性。
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: 除了设置模型关系和
accepts_nested_attributes_for
,还可以在ActiveAdmin资源中尝试以下操作:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.