将模型管理添加到 Active Admin - Rails 3

Add model administration to Active Admin - Rails 3

我对 Rails 和 ActiveAdmin 还很陌生。

我希望有一个类似于 Django 管理界面的界面,带有我的应用程序模型,这样我就可以管理产品和其他东西。

到目前为止,我有 admin_users url 可以在我的应用程序中添加或删除管理员用户,这太棒了。

我正在使用 Rails 3,我想知道是否可以在用户之外添加一个新菜单,以便我可以管理 dashboard

中的其他模型

我试过了rails generate active_admin:resource Product

它在 app/admin/ 上创建了一个名为 product.rb 的文件,但它不起作用,这是我的 Product 模型 product.rb

class Product < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, 
     :recoverable, :rememberable, :trackable, :validatable

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
belongs_to :category
has_many :line_items
has_many :orders, through: :line_items

validates_presence_of :category_id, :name, :price_cents
attr_accessible :avatar
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/

attr_accessor :price

attr_accessible :category_id, :description, :image, :name, :price_cents, :upc_code, :price, :taxable

def price
  price_cents/100.0 if price_cents
end

def price= price
  self.price_cents = (price.to_f*100).round
end

end

我不知道,我做错了什么?

有什么想法吗?

要注册您的 Product 模型,运行:

rails generate active_admin:resource Product

这会在 app/admin/product.rb 处创建一个用于配置资源的文件。 刷新您的网络浏览器以查看界面。

请查看 Active Admin Documentation 了解更多详情。

app/admin 下您的 product.rb 文件注册模型。我会看起来像

ActiveAdmin.register Product do
  :category_id, :description, :image, :name, :price_cents, :upc_code, :price, :taxable

  index do
    selectable_column
    id_column
    column :description
    column :name
    column :price_cents
    actions
  end

  form do |f|
    f.inputs "Product Details" do
      f.input :price
      f.input :name
      f.input :description
      # more fields
    end
    f.actions
  end
end

查看 documentation 了解更多信息。