简体   繁体   English

在ActiveAdmin中的before_filter中填充表单的集合

[英]Populate a collection for a form in a before_filter in ActiveAdmin

I am trying to populate a collection (@displayable_collection) to select from in my form 我正在尝试填充要在我的表单中选择的集合(@displayable_collection)
(this is a simplified version of what will eventually go into @displayable_collection (这是最终会放入@displayable_collection的简化版本

Here is my code... but it isn't working ..... what am I doing wrong? 这是我的代码...但是它不起作用.....我在做什么错?

controller do
  before_filter :populate_collection, :only => [:new, :edit]

  private
  def populate_collection
    @displayable_collection = ArtItem.all.map{ |item| [item.to_s, "#{item.class}_#{item.id}"]}
  end
end

form do |f|
  f.inputs 'Details' do
    f.input :project
    f.input :name
  end

  f.inputs 'Display Items' do
    f.has_many :display_items do |item|
      item.input :displayable_combined_fields, :collection => @displayable_collection
      item.input :location, :input_html => {:selected => location.id}
      item.input :height
      item.input :width
      item.input :depth
    end
  end
  f.actions
end

So the Answer is that you reference the instance variable as a helper method. 因此,答案是您将实例变量作为辅助方法进行引用。 I know it sounds weird, but it works. 我知道这听起来很奇怪,但是可以。

Documented here: https://github.com/gregbell/active_admin/issues/2298 记录在这里: https : //github.com/gregbell/active_admin/issues/2298

controller do
  before_filter :populate_collection, :only => [:new, :edit]

  private
  def populate_collection
    @displayable_collection = ArtItem.all.map{ |item| [item.to_s, "#{item.class}_#{item.id}"]}
  end
end

form do |f|
  f.inputs 'Details' do
    f.input :project
    f.input :name
  end

  f.inputs 'Display Items' do
    f.has_many :display_items do |item|
      item.input :displayable_combined_fields, :collection => displayable_collection
      item.input :location, :input_html => {:selected => location.id}
      item.input :height
      item.input :width
      item.input :depth
    end
  end
  f.actions
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM