Ruby-on-Rails中Avo的使用教程

360 阅读3分钟

Avo概述

AvoHQ的主要承诺是去除模板。它是Rails administrate gem的一个替代品。

Avo的主要目的是为Rails应用程序管理数据库记录。为了做到这一点,它使用了与模型相对应的资源概念。一旦定义了一个资源,用户就可以映射数据库字段,这样Avo就知道要显示什么数据以及如何显示。在Rails中,index是用来显示一个资源的所有实例,show是用来显示一个特定的实例,而edit/create是用来编辑和创建一个特定的实例。因此,Avo中的每个字段声明都会在索引视图中增加一列数据,在显示、编辑和创建视图中增加一行。令人惊讶的是,Avo并不只是支持基本的字段,他们还能处理复杂的字段,如trix、markdown、gravatar、boolean_group、file等。一旦这些资源被设置了所需的字段,就可以通过使用Avo的过滤器,根据条件对它们进行过滤。Avo也有一个叫做行动的东西,可以做一些事情,比如说将转换应用到资源。总而言之,它是一个相当强大和方便的工具。请注意,它的高级功能不是免费的。

利用Avo创建一个Rails应用程序

现在你对Avo有了一个概念,是时候把这些知识付诸行动了

创建一个新的Rails应用程序并安装Avo

$> ruby -v  
ruby 3.0.1p64 # Ruby >= 2.7 
$> rails -v  
Rails 7.0.2.4 # Ruby-on-Rails >= 6.0
$> bundle -v  
Bundler version 2.3.14 # Bundler 2.xx

现在进入你的工作区,输入

bin/rails new avo_test
cd avo_test

现在在gemfile中,添加

gem "avo"

然后运行这个命令来完成设置!

bundle install
bin/rails generate avo:install # Generate initializer and add Avo to routes.rb

在数据库中加入种子,这样我们就有了可以使用的信息

运行这些命令,为书籍和作者创建迁移。

bin/rails generate migration createBooks
bin/rails generate migration createAuthors
# Inside of db/migrate/..._create_authors.rb
class CreateAuthors < ActiveRecord::Migration[7.0]
  def change
    create_table :authors do |t|
      t.string :first_name, null: false
      t.string :last_name, null: false
      t.string :email, null: false
      t.timestamps
    end
    add_index :authors, :email, unique: true
  end
end
# Inside of db/migrate/..._create_books.rb
class CreateBooks < ActiveRecord::Migration[7.0]
  def change
    create_table :books do |t|
      t.string :title, null: false
      t.integer :author_id, null: false
      t.timestamps
    end
    add_index :books, :author_id
  end
end

现在运行这个命令

bin/rails db:migrate

现在为这些表创建相应的模型!

# Inside of app/models/author.rb
class Author < ApplicationRecord
  validates :first_name, :last_name, :email, presence: true
  validates :email, uniqueness: true
  has_many :books
end
# Inside of app/models/book.rb
class Book < ApplicationRecord
  validates :title, :author_id, presence: true
  
  belongs_to :author
end

现在我们终于可以给数据库播种了!

# Inside of db/seeds.rb
author1 = Author.create(first_name: "Tom", last_name: "Pratt", email: "tp@gmail.com")
author2 = Author.create(first_name: "John", last_name: "Smith", email: "js@gmail.com")
author3 = Author.create(first_name: "Jane", last_name: "Doe", email: "jd@gmail.com")
book1 = Book.create(title: "The Great Gatsby", author_id: author1.id)
book2 = Book.create(title: "The Catcher in the Rye", author_id: author1.id)
book3 = Book.create(title: "The Grapes of Wrath", author_id: author2.id)

然后运行

bin/rails db:seed

设置主页以查看管理仪表板

bin/rails g controller home # Creates a home controller 
echo '<h1>Hello world!</h1>' > app/views/home/index.html.erb 

并使默认路由成为你刚刚创建的主页!

# Inside of config/routes.rb
Rails.application.routes.draw do
  mount Avo::Engine, at: Avo.configuration.root_path
  
  root "home#index"
end

生成你的第一个Avo资源

从定义资源开始!这个命令将在app/avo/resources目录下生成一个资源文件。

bin/rails generate avo:resource Author # Creates author_resource.rb
bin/rails generate avo:resource Book # Creates book_resource.rb

现在让我们定义一些要显示的字段,因为默认只显示id。

# Inside of author_resource.rb
class AuthorResource < Avo::BaseResource
  self.title = :id
  self.includes = []
  field :id, as: :id
  field :first_name, as: :text
  field :last_name, as: :text
  field :email, as: :text
end
# Inside of book_resource.rb
class BookResource < Avo::BaseResource
  self.title = :id
  self.includes = []
  field :id, as: :id
  field :title, as: :text
  field :author, as: :belongs_to
end

现在一切都设置好了,使用命令

bin/rails server

并在你的浏览器中导航到http://localhost:3000/avo,查看管理面板!

点击页面左上角的汉堡包图标,你可以看到定义的资源;作者和书籍。 image.png

Avo菜单

使用Avo资源管理数据库记录

从侧边栏,点击要管理的资源,你就会到达这个简洁的用户界面。

例如,点击书籍资源。

image.png

Avo书籍

注意它已经被我们的种子数据填满了!

从这里,你可以很容易地:创建一个新书,删除记录,编辑它,并单独查看它。你也可以选择多个或所有的书,一次性编辑/删除它们。这真是一个管理面板,使Rails应用程序的数据库记录管理变得简单而容易。这个简单的演示并没有真正显示出:复杂类型、过滤、动作、网格视图等的威力。请仔细阅读 Avo的文档,并尝试使用它的更多高级功能!我建议看一下 Avo的演示应用demo app,看看这个框架能做什么。

结论

到目前为止,我们在Bootrails的选择是坚持使用普通的Rails(加上CSS框架和预制设计)来构建管理部分。 然而,看到其他工具如何处理这个问题总是很有趣。 Avo为管理Rails应用程序中的数据库记录快速创建了漂亮的管理面板。