Ruby on Rails---Active Admin使用(一)

时间:2021-11-23 05:38:43

概述

Active Admin提供了一个友好的后台管理界面,将CRUD等操作可视化,操作极其方便

安装

1. 添加gem

gem "devise", :github => 'plataformatec/devise', :branch => 'master'
gem 'activeadmin', github: 'activeadmin'
gem 'bcrypt', '~> 3.1.7'
gem 'bcrypt-ruby', '3.1.1.rc1', :require => 'bcrypt'

2. 执行安装命令

rails generate active_admin:install

3. 迁移数据库

rake db:migrate

4. 启动rails

ip/admin即可

5. 添加登陆用户

进入rails console,(生产环境要加 -e production)
AdminUser.create!(:email => 'xxx@qq.com', :password => 'mobin123', :password_confirmation => 'mobin123')

6. 注册model到Active Admin中

rails generate active_admin:resource modelname,之后刷新浏览器即可

安装中遇到的问题:

  • 错误1:To use devise you need to specify it in your Gemfile. If you don't want to use devise, run the generator with --skip-users. (ActiveAdmin::GeneratorError)
    解决办法:gem "devise", :github => 'plataformatec/devise', :branch => 'master'

  • 错误2: 1.9 / bcrypt_ext(LoadError)(cannot load such file -- 1.9/bcrypt_ext (LoadError))
    解决办法:gem 'bcrypt', '~> 3.1.7'
    gem 'bcrypt-ruby', '3.1.1.rc1', :require => 'bcrypt'

  • 错误3: Active Admin前端无法渲染
    解决办法:对项目重新进行预编译
    rake assets:precompile

参考资料
Active Admin--Geting started