使用Rails Gem Active Admin和关联

时间:2011-05-16 23:00:52

标签: ruby-on-rails-3 ruby-on-rails-4 rubygems activeadmin

我正在尝试新的Rails gem http://activeadmin.info/,它的效果非常好!但是,我找不到任何关于如何跨关联使用它的文档。例如:

class Membership < ActiveRecord::Base
  belongs_to :course
  belongs_to :person

class Course < ActiveRecord::Base
  has_many :memberships
  has_many :people,  :through => :memberships

class Person < ActiveRecord::Base
  has_many :memberships
  has_many :courses, :through => :memberships

会员加入表还包括一些额外数据(即:出勤率)。我正在尝试使用课程和学生名称显示成员资格 - 并允许对这些名称进行过滤/排序。据我所知,Active Admin不能跨关联工作。有没有其他人成功做到这一点,或发现另一个宝石呢?非常感谢!

4 个答案:

答案 0 :(得分:5)

<强> ingredient.rb

class Ingredient < ActiveRecord::Base
has_and_belongs_to_many :products, :join_table => :ingredients_products
end

<强> product.rb

class Product < ActiveRecord::Base
  has_and_belongs_to_many :ingredients, :join_table => :ingredients_products
end

不要忘记加入表的迁移(:id为false!)

class CreateProductsIngredients < ActiveRecord::Migration
  def self.up
    create_table :ingredients_products,:id => false do |t|
      t.integer :product_id
      t.integer :ingredient_id
      t.timestamps
    end
  end

  def self.down
    drop_table :products_ingredients
  end
end

现在在ActiveAdmin资源中定义表单,覆盖默认

ActiveAdmin.register Product do
  form do |f|
        f.inputs "Details" do
          f.input :product_name
          f.input :brand
          f.input :ingredients # don't forget this one!
        end
end

答案 1 :(得分:4)

我一直在玩ActiveAdmin一段时间了,这就是我如何设法让关联在索引和表单中工作。

我刚刚猜到了下面的一些模型列。另请注意,在表格中。 'person'部分将显示所有要编辑的列,而'course'部分将仅显示指定的列。

ActiveAdmin.register User do
    index do
        column :id
        column :name
        column :attendance
        column :person do |membership|
            membership.person.name
        end
        column :course do |membership|
            membership.course.name
        end
        default_actions
    end

    form do |f|
        f.inputs "Membership" do
            f.input :name
            f.input :created_at
            f.input :updated_at
        end
        f.inputs :name => "Person", :for => :person do |person|
            person.inputs
        end
        f.inputs :name => "Course", :for => :course do |course|
            course.input :name
        end
        f.buttons
    end
end

我没有对此进行测试,但您应该能够将这些想法应用到您的案例中。它适合我的。

更新:我刚刚再次阅读了您的问题,并指出您希望能够对关联列进行排序。我刚检查了我的实现,这确实无法正常工作。我的回答可能对你没用,但无论如何我都会留在这里(可能会帮助其他人)。

答案 2 :(得分:1)

我自己刚刚开始使用这个gem,虽然我还没有显示关联信息,但是这里是如何为关联创建表单的:

form do |f|
  f.inputs

  f.has_many :associations do |association|
     association.inputs
  end

  f.buttons
end

这将为您提供脚手架的基本形式。

答案 3 :(得分:0)

<强> ingredient.rb

class Ingredient < ActiveRecord::Base
   has_and_belongs_to_many :products, :join_table => :ingredients_products
end

<强> product.rb

class Product < ActiveRecord::Base
  attr_accessible ingredient_ids
  has_and_belongs_to_many :ingredients, :join_table => :ingredients_products
end

<强> migration_xxx.rb

class CreateProductsIngredients < ActiveRecord::Migration
  def self.up
    create_table :ingredients_products,:id => false do |t|
      t.integer :product_id
      t.integer :ingredient_id
      t.timestamps
    end
  end

  def self.down
    drop_table :products_ingredients
  end
end

<强> products.rb

ActiveAdmin.register Product do
  form do |f|
     f.inputs "Details" do
        f.input :product_name
        f.input :brand
        f.input :ingredients
     end
  end
  ...
end
相关问题