【问题标题】:Add model administration to Active Admin - Rails 3将模型管理添加到 Active Admin - Rails 3
【发布时间】:2015-08-24 11:59:43
【问题描述】:

我对 Rails 和 ActiveAdmin 还很陌生。

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

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

我正在使用 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

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

有什么想法吗?

【问题讨论】:

标签: ruby-on-rails ruby django ruby-on-rails-3 activeadmin


【解决方案1】:

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

rails generate active_admin:resource Product

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

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

【讨论】:

  • 是的,我这样做了,但之前没有工作,大声笑,非常感谢!
  • 不客气 :) 我很高兴它现在为你工作 :-)
【解决方案2】:

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 了解更多信息。

【讨论】:

  • 谢谢,我将把它作为个人文档,非常感谢!
猜你喜欢
  • 1970-01-01
  • 2014-07-14
  • 1970-01-01
  • 2012-10-12
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多