这几天在弄网页后端,走了很多歪路,在此发表这篇帖子,希望像我一样的新手在最开始接触 activeadmin 时可以少走点歪路。 activeadmin 最后能够实现的效果:admin 可以查看、编辑、新建、删除所有用户,而 user 只能查看所有用户。
现在开始 activeadmin 之旅吧!
ruby
gem 'devise'
gem 'activeadmin', github: 'gregbell/active_admin'
gem 'cancancan'
当初天真的我就只加了 gem 'activeadmin',没加后面那串地址,就会出现下面这样的鬼:Fetching version metadata from https://rubygems.org/..
Resolving dependencies....
Your Gemfile requires gems that depend on each other, creating an infinite loop.
Please remove gem 'meta_search' and try again.
2..bundle install 3.rails g active_admin:install User 原先我是先 rails generate active_admin:install,然后 rails destroy model AdminUser,最后 rails generate active_admin:resource User,具体细节请看http://codeonhill.com/devise-cancan-and-activeadmin/,按教程上这样做也没什么问题,可是登录界面会比较丑,然后头儿默默表示前端太丑,纠结了很久,自己改了样式,头儿还是觉得丑,于是就重建了一个项目重做。 4.在 config/environments/development.rb 中加入:
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
5.在 app/views/layouts/application.html.erb 的<%= yield %>前加入:
<p><%= notice %></p>
<p><%= alert %></p>
6.运行:
$ rails g devise:install
7.运行:
$ rails g devise User
8.在 user 数据库中加一 role 字段,运行:
rails g migration add_role_to_users role:string
9.在 db/migrate/XXX_add_role_to_users.rb 中替换内容如下:
class AddRoleToUsers < ActiveRecord::Migration
def change
add_column :users, :role, :string
User.create! do |u|
u.email = '[email protected]'
u.password = 'adminadmin'
u.role = 'admin'
end
end
end
10.运行:
rake db:migrate
会报错没有“role=”这个方法,遇此错误可以先把这一行先注释掉。 11.运行 rails s,访问 localhost:3000/admin,然后你就可以看到 activeadmin 了 12.运行
rails g cancan:ability
cancan 和 activeadin 的联用看了很多民间教程,结果会报错,最后终于找到官方文档http://activeadmin.info/docs/13-authorization-adapter.html,看来还是官方文档靠谱点 13.在 Active Admin initializer 中加入
config.authorization_adapter = ActiveAdmin::CanCanAdapter
config.on_unauthorized_access = :access_denied
14.access_denied 方法在 application_controller.rb 中定义
class ApplicationController < ActionController::Base
protect_from_forgery
def access_denied(exception)
redirect_to admin_organizations_path, :alert => exception.message
end
end
15.在 app/models/ability.rb中添加你所想要的权限
class Ability
include CanCan::Ability
def initialize(user)
user | | = User.new
if user.role == 'admin'
can :manage, :all
else
can [:read,:destroy], :all
can [:create,:update], :Post
cannot [:create,:update],:User
end
end
现在这个教程就结束了,大概应该就是这样,希望没有遗漏。