2011-05-16 31 views
8

我正在尝试新的Rails gem http://activeadmin.info/,它的工作很棒!不过,我找不到关于如何在关联中使用它的任何文档。例如:使用Rails的Gem活动管理与关联

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并不适用于各种关联。有没有其他人成功地做到这一点,或发现另一个宝石呢?非常感谢!

回答

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 

现在在你的Acti中定义表单veAdmin资源,覆盖默认

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 
+0

感谢您的答复 - 你从一些文件我错过了得到这个?如果是这样 - 在哪里?问题是,如果我按照你的要求去做,我会得到一个说“成分”的专栏。我不了解你 - 但那不是我想要的。我可以指定要显示的关联列吗? – Tyler 2011-05-21 17:41:07

+0

嗨泰勒,我从简单的表单gem文档中得到它,似乎他们正在使用这个。我也认为我误解了你的问题。我没有机会去做你正在尝试的东西,但我自己会为“成分”定义一个虚拟属性,或者覆盖名称属性,从而达到如下目的: 'def self.name “Ingredient#{self .name}“ end' 我没有自己尝试过,所以无法承诺,如果它能为你工作 – 2011-06-02 21:24:40

+0

ActiveAdmin使用Formtastic(github.com/justinfrench/formtastic),而不是SimpleForm。在“形式”块中,几乎所有的Formtastic都在做这项工作。 Formtastic可能也会处理这些关联,所以我建议您查看他们的文档以获得一些指示:) – 6twenty 2011-06-08 10:37:51

1

我刚刚用这个宝石我开始了,而我还没有得到解决,以显示关联信息,这里是你如何创建一个社团形式:

form do |f| 
    f.inputs 

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

    f.buttons 
end 

这将给你一个脚手架的基本形式。

+5

我试过这种方法。但是一旦我添加“f.has_many”块,就会出现一个错误“未定义的方法'new_record?'为零:NilClass“。你有什么建议吗? – 2011-08-18 05:08:01

+0

您需要在模型中添加'accep_nested_attributes_for',然后它应该可以工作。 – 2012-08-29 08:31:07

4

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

我刚刚猜到了下面的一些模型列。另请注意,在表单中。 “人员”部分将显示所有要编辑的列,而“课程”部分将显示指定的列。

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 

我还没有测试过这个,但你应该能够将这些想法应用到你的案例。它为我的工作。

更新:我刚刚读过您的问题,并指出您希望能够对关联列进行排序。我刚刚检查了我的实现,这确实不起作用。我的回答对你来说可能没用,但无论如何我会把它留在这里(可能会帮助其他人)。

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 

产品。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 
相关问题