![](/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.