【问题标题】:ActiveAdmin association doesn't update its attributes in saveActiveAdmin 关联不会在保存中更新其属性
【发布时间】: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


【解决方案1】:

试试看

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

【讨论】:

  • 这给了我两个问题。 1) 它在 inverse_of 上出错,我可以通过将冒号添加到 "inverse_of:" 来修复它,2) 未定义的方法 "has_one",我不知道如何修复。
  • @JamesTesta 实际上我从activeadmin.info/docs/5-forms.html 看到了 f.has_many,所以我认为 has_one 也应该可以工作。
  • 我试过了,但关联属性仍然没有更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-03
  • 1970-01-01
  • 2020-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-13
相关资源
最近更新 更多