![](/img/trans.png)
[英]How to render another resource's form in active admin dashboard - Rails?
[英]How can I use Active Admin's form block to update a table that tracks when an attribute in a resource is updated?
如何使用Active Admin的表单块来更新跟踪当前资源中特定属性何时更新的表?
例如,我有这样的事情:
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
其中,除了new_comment
所有内容都是当前资源上存在的属性。
我尝试在资源类中执行以下操作:
def new_comment
history = History.new(:id, Time.now)
history.comment
end
当失败时,我也尝试创建一个控制器实例变量,但这也不起作用。 场所类与历史类之间并没有任何关联。 我只是想创建一个输入列,每当用户编辑场所然后添加评论时,该列都会在“历史记录”表中创建新行。
历史记录模型如下所示:
class History < ActiveRecord::Base
attr_accessible :comment, :venue_id, :created_at
belongs_to :venue
def initialize(id, datetime)
@id = id
@datetime = datetime
end
end
表单用于如下所示的场所:
class Venue < ActiveRecord::Base
attr_accessible :name, :address, :zip, :city
has_many :histories
accepts_nested_attributes_for :histories
//with a bunch of methods
end
经过一些挖掘,我尝试这样做,但是它也不起作用:
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
除了设置模型关系和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.