Tyl*_*ler 8 rubygems ruby-on-rails-3 activeadmin ruby-on-rails-4
我正在尝试新的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
Run Code Online (Sandbox Code Playgroud)
会员加入表还包括一些额外的数据(即:出勤).我正在尝试使用课程和学生名称显示成员资格 - 并允许对这些名称进行过滤/排序.据我所知,Active Admin不能跨关联工作.有没有其他人成功做到这一点,或发现另一个宝石呢?非常感谢!
小智 5
ingredient.rb
class Ingredient < ActiveRecord::Base
has_and_belongs_to_many :products, :join_table => :ingredients_products
end
Run Code Online (Sandbox Code Playgroud)
product.rb
class Product < ActiveRecord::Base
has_and_belongs_to_many :ingredients, :join_table => :ingredients_products
end
Run Code Online (Sandbox Code Playgroud)
不要忘记加入表的迁移(: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
Run Code Online (Sandbox Code Playgroud)
现在在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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11388 次 |
| 最近记录: |