将 Rails Gem Active Admin 与关联结合使用

发布于 2024-11-07 23:50:01 字数 612 浏览 0 评论 0原文

我正在尝试新的 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 不能跨关联工作。有没有其他人成功地做到了这一点,或者找到了另一个能做到这一点的宝石?非常感谢!

I'm trying out the new Rails gem http://activeadmin.info/ and it's working great! However I can't find any documentation on how to use it across associations. For example:

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

The membership join table includes some extra data as well (ie: attendance). I'm trying to show the membership with both the course and student name - and allow filtering / sorting on those names. As far as I have found, Active Admin doesn't work across associations. Has anyone else been successful in doing that, or found another gem that does? Thanks so much!

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

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

发布评论

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

评论(4

锦欢 2024-11-14 23:50:01

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

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

don't forget the migrations for the joining table (:id to 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

Now define the form in you ActiveAdmin resource, override the default

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
笨死的猪 2024-11-14 23:50:01

我已经使用 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

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

更新:我刚刚再次阅读了您的问题,并注意到您希望能够对关联列进行排序。我刚刚检查了我的实现,这确实不起作用。我的答案可能对你没用,但无论如何我都会把它留在这里(可能对其他人有帮助)。

I've been playing with ActiveAdmin for a while now, here's how I managed to get associations to work in Indexes and Forms.

I've just guessed some of your model columns below. Also note, in the form. The 'person' section will show all the columns for editing, whereas the 'course' section will just show the specified column.

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

I haven't tested this, but you should be able to apply these ideas to your case. It's working for mine.

Update: I've just read your question again and noted that you're wanting to be able to sort on the association column. I've just checked my implementation and this indeed is not working. My answer may be useless to you but I'll leave it here anyway (might help someone else).

小女人ら 2024-11-14 23:50:01

我自己刚刚开始使用这个 gem,虽然我还没有抽出时间来显示关联信息,但以下是创建关联表单的方法:

form do |f|
  f.inputs

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

  f.buttons
end

这将为您提供带有脚手架的基本表单。

I've just started using this gem myself, and while I haven't gotten around to showing association information, here's how you create a form for associations:

form do |f|
  f.inputs

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

  f.buttons
end

That will give you a basic form with scaffolding.

过去的过去 2024-11-14 23:50:01

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

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