ActiveAdmin关联在保存时不更新其属性

时间:2014-02-22 16:06:08

标签: ruby-on-rails associations activeadmin formtastic

保存初始模型时,我无法让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模型。

有谁知道如何让Endpoint模型更新它 属性或我需要修复的内容?

1 个答案:

答案 0 :(得分:0)

试一试

form do |f|
    f.inputs "Account" do
      f.input :name
    end

    f.inputs do
      f.has_one :endpoint, inverse_of :account, class_name: 'Abcd::Endpoint' do |nested_form|
         nested_form.input :access_key,
         nested_form.label('Access key')
      end
    end
  f.actions
end