Ruby-on-rails 具有has_many-belongs_to关系的ActiveAdmin表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7958009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:16:18  来源:igfitidea点击:

ActiveAdmin forms with has_many - belongs_to relationships?

ruby-on-railsruby-on-rails-3.1activeadmin

提问by agente_secreto

I have the models Home and Photo, which have a has_many - belongs_to relationship (a polymorphic relationship, but I dont think that matters in this case). I am now setting up active admin and I would like admins to be able to add photos to homes from the homes form.

我有模型 Home 和 Photo,它们有一个 has_many -belongs_to 关系(一种多态关系,但我认为在这种情况下不重要)。我现在正在设置活动管理员,我希望管理员能够从家庭表单向家庭添加照片。

The photos are managed by the CarrierWave gem, which I dont know if will make the problem easier or harder.

照片由 CarrierWave gem 管理,我不知道这是否会使问题变得更容易或更难。

How can I include form fields for a different model in the Active Admin Home form? Any experience doing something like this?

如何在 Active Admin Home 表单中包含不同模型的表单字段?有做这样的事情的经验吗?

class Home < ActiveRecord::Base
  validates :name, :presence => true,
                     :length => { :maximum => 100 }
  validates :description, :presence => true      
  has_many :photos, :as => :photographable

end


class Photo < ActiveRecord::Base
    belongs_to :photographable, :polymorphic => true
    mount_uploader :image, ImageUploader
end

回答by jfedick

Try something like this in app/admin/home.rb:

在 app/admin/home.rb 中尝试类似的操作:

form do |f|
  f.inputs "Details" do
    f.name
  end

  f.has_many :photos do |photo|
    photo.inputs "Photos" do
      photo.input :field_name 
      #repeat as necessary for all fields
    end
  end
end

Make sure to have this in your home model:

确保在您的家庭模型中有这个:

accepts_nested_attributes_for :photos

I modified this from another stack overflow question: How to use ActiveAdmin on models using has_many through association?

我从另一个堆栈溢出问题修改了这个:How to use ActiveAdmin on models using has_many through association?

回答by MegaCasper

You could try this:

你可以试试这个:

form do |f|
  f.semantic_errors # shows errors on :base
  f.inputs          # builds an input field for every attribute

  f.inputs 'Photos' do
    f.has_many :photos, new_record: false do |p|
      p.input :field_name
      # or maybe even
      p.input :id, label: 'Photo Name', as: :select, collection: Photo.all
    end
  end

  f.actions         # adds the 'Submit' and 'Cancel' buttons  
end

Also, you can look at https://github.com/activeadmin/activeadmin/blob/master/docs/5-forms.md(See Nested Resources)

此外,您可以查看https://github.com/activeadmin/activeadmin/blob/master/docs/5-forms.md(请参阅嵌套资源

回答by rangalo

I guess you are looking for a form for a nested model. Take a look at following railscasts.

我猜您正在寻找嵌套模型的表单。看看下面的railscasts。

http://railscasts.com/episodes/196-nested-model-form-part-1

http://railscasts.com/episodes/197-nested-model-form-part-2

http://railscasts.com/episodes/196-nested-model-form-part-1

http://railscasts.com/episodes/197-nested-model-form-part-2

I cannot tell you much about active_admin, but I think this should not make a difference in handling the nested model.

我不能告诉你很多关于 active_admin 的信息,但我认为这对处理嵌套模型应该没有影响。

回答by John Mount

I have a has_one model, like this:

我有一个 has_one 模型,如下所示:

f.has_many :addresses do |a|
  a.inputs "Address" do
    a.input :street  ... etc.

While this correctly reflects our associations for Address(which is a polymorphic model) using f.has_onefails. So I changed over to has_manyand all's well. Except now we have to prevent our users from creating multiple addresses for the same entity.

虽然这正确地反映了我们对Address(这是一个多态模型)使用f.has_one失败的关联。所以我换了has_many,一切都很好。除了现在我们必须阻止我们的用户为同一个实体创建多个地址。