【发布时间】:2014-02-22 16:06:08
【问题描述】:
保存初始模型时,我无法让 ActiveAdmin 保存关联模型。
我有两个看起来像这样的模型:
# app/models/account.rb
class Account < ActiveRecord::Base
has_one :endpoint, inverse_of :account, class_name: 'Abcd::Endpoint'
accepts_nested_attributes_for :endpoint
delegate :access_key, to: :endpoint
end
# app/models/abcd/endpoint.rb
class Abcd::Endpoint < ActiveRecord::Base
attr_accessible :account_id, :access_key
belongs_to :account
end
我的 ActiveAdmin 文件如下所示:
# app/admin/account.rb
Activeadmin.register Account do
form do |f|
f.inputs do
f.input :name
end
f.inputs title: 'endpoints', for: [:endpoint. f.object.endpoint || Endpoint.new] do |nested_form|
nested_form.input :access_key,
label: 'Access Key',
as: :string
end
f.actions
end
show do |account|
row 'endpoint has access_key' do
account.access_key
end
end
end
当我点击“更新帐户”时,帐户会更新,但端点 模型没有更新。看来端点属性不是 被发送到 Endpoint 模型。
有谁知道如何让 Endpoint 模型更新它的 属性还是我需要修复什么?
【问题讨论】:
-
不应该是nested_form.input 而不是nested.form.input
-
对不起,这是我的错字。我在 app/admin/account.rb 文件代码中拥有的是 nested_form.input。更新了问题。
-
如果将
Endpoint.new替换为f.object.build_endpoint会发生什么? -
我试过了,端点实例的属性仍然没有更新。
标签: ruby-on-rails associations activeadmin formtastic