active_admin 中的嵌套表单,带有选择或创建选项

发布于 2024-12-01 06:09:11 字数 701 浏览 2 评论 0原文

我们使用 active_admin 作为我们的管理后端。

我们有一个模型“App”:属于模型“Publisher”:

class App < ActiveRecord::Base
  belongs_to :publisher
end

class Publisher < ActiveRecord::Base
  has_many :apps
end

为“App”模型创建新条目时,我希望可以选择选择现有发布者或(如果尚未创建发布者)创建以相同(嵌套)形式(或至少不离开页面)的新发布者。

有没有办法在 active_admin 中执行此操作?

这是我们到目前为止所得到的(在 admin/app.rb 中):

form :html => { :enctype => "multipart/form-data" } do |f|
  f.inputs do
    f.input :title
    ...
  end

  f.inputs do
    f.semantic_fields_for :publisher do |p| # this is for has_many assocs, right?
      p.input :name
    end
  end

  f.buttons
end

经过几个小时的搜索,我将不胜感激任何提示......谢谢!

We are using active_admin for our administration backend.

We have a model "App" that :belongs_to model "Publisher":

class App < ActiveRecord::Base
  belongs_to :publisher
end

class Publisher < ActiveRecord::Base
  has_many :apps
end

When creating a new entry for the "App" model I want to have the option to either select an existing publisher or (if the publisher is not yet created) to create a new publisher in the same (nested) form (or at least without leaving the page).

Is there a way to do this in active_admin?

Here's what we have so far (in admin/app.rb):

form :html => { :enctype => "multipart/form-data" } do |f|
  f.inputs do
    f.input :title
    ...
  end

  f.inputs do
    f.semantic_fields_for :publisher do |p| # this is for has_many assocs, right?
      p.input :name
    end
  end

  f.buttons
end

After hours of searching, I'd appreciate any hint... Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

度的依靠╰つ 2024-12-08 06:09:11

首先,确保在您的发布者模型中您拥有关联对象的正确权限:

class App < ActiveRecord::Base
  attr_accessible :publisher_attributes

  belongs_to :publisher
  accepts_nested_attributes_for :publisher, reject_if: :all_blank
end

然后在您的 ActiveAdmin 文件中:

form do |f|
  f.inputs do
    f.input :title
    # ...
  end

  f.inputs do
    # Output the collection to select from the existing publishers
    f.input :publisher # It's that simple :)

    # Then the form to create a new one
    f.object.publisher.build # Needed to create the new instance
    f.semantic_fields_for :publisher do |p|
      p.input :name
    end
  end

  f.buttons
end

我在我的应用程序中使用了稍微不同的设置(而是 has_and_belongs_to_many 关系),但我设法让它工作为我。如果此代码输出任何错误,请告诉我。

First, make sure that in your Publisher model you have the right permissions for the associated object:

class App < ActiveRecord::Base
  attr_accessible :publisher_attributes

  belongs_to :publisher
  accepts_nested_attributes_for :publisher, reject_if: :all_blank
end

Then in your ActiveAdmin file:

form do |f|
  f.inputs do
    f.input :title
    # ...
  end

  f.inputs do
    # Output the collection to select from the existing publishers
    f.input :publisher # It's that simple :)

    # Then the form to create a new one
    f.object.publisher.build # Needed to create the new instance
    f.semantic_fields_for :publisher do |p|
      p.input :name
    end
  end

  f.buttons
end

I'm using a slightly different setup in my app (a has_and_belongs_to_many relationship instead), but I managed to get it working for me. Let me know if this code outputs any errors.

谷夏 2024-12-08 06:09:11

form_builder 类支持名为 has_many 的方法。

f.inputs do
  f.has_many :publisher do |p|
    p.input :name
  end
end

那应该可以完成工作。

更新:我重新阅读了您的问题,这只允许添加新的发布者,但我不确定如何进行选择或创建。

The form_builder class supports a method called has_many.

f.inputs do
  f.has_many :publisher do |p|
    p.input :name
  end
end

That should do the job.

Update: I re-read your question and this only allows to add a new publisher, I am not sure how to have a select or create though.

萌化 2024-12-08 06:09:11

根据 ActiveAdmin:http://activeadmin.info/docs/5-forms.html

您只需要执行以下操作:

f.input :publisher

According to ActiveAdmin: http://activeadmin.info/docs/5-forms.html

You just need to do as below:

f.input :publisher
少钕鈤記 2024-12-08 06:09:11

我发现你需要做三件事。

为表单添加语义字段

f.semantic_fields_for :publisher do |j|
  j.input :name
end

将nested_belongs_to语句添加到控制器

controller do
    nested_belongs_to :publisher, optional: true
end

更新控制器上允许的参数以接受参数,使用关键字属性

permit_params publisher_attributes:[:id, :name]

I've found you need to do 3 things.

Add semantic fields for the form

f.semantic_fields_for :publisher do |j|
  j.input :name
end

Add a nested_belongs_to statement to the controller

controller do
    nested_belongs_to :publisher, optional: true
end

Update your permitted parameters on the controller to accept the parameters, using the keyword attributes

permit_params publisher_attributes:[:id, :name]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文